How to Self-Host n8n with Docker Compose and Postgres

November 13, 2026

You switch n8n from the default SQLite file to a Postgres database by setting DB_TYPE=postgresdb plus four connection variables in a Compose file, then recreating the container. Plan on 45 minutes, including HTTPS and a backup cron job. Do this before you go past a few hundred executions a day; SQLite locks on concurrent writes and you will see random “database is locked” errors.

n8n runs fine on SQLite for testing. Production is a different job, and the swap is not complicated once you know which variables matter.

Why does SQLite break at scale?

Because SQLite allows a single writer. When two workflows finish at the same second, the second one waits, and under load it fails with a write lock error instead of retrying politely.

In practice this shows up as scheduled workflows silently skipping, or executions landing in the log with a red “database is locked” banner. Users hit it somewhere between a few thousand and a few tens of thousands of executions per day. The threshold depends on write concurrency, not volume, which is why two teams with similar volumes report very different experiences.

Postgres removes the ceiling. It also unlocks queue mode.

What goes in the Compose file?

Four services: Postgres, the n8n main process, a queue worker and a reverse proxy. For a small setup, drop the worker and run main only.

services:
  postgres:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: change-me
      POSTGRES_DB: n8n
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n"]
      interval: 10s

  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_USER: n8n
      DB_POSTGRESDB_PASSWORD: change-me
      N8N_ENCRYPTION_KEY: paste-a-32-char-random-string
      N8N_HOST: n8n.example.com
      WEBHOOK_URL: https://n8n.example.com/
      GENERIC_TIMEZONE: Asia/Shanghai
    ports:
      - "5678:5678"
    volumes:
      - n8n_data:/home/node/.n8n

volumes:
  pgdata:
  n8n_data:

Three of those variables carry most of the weight. N8N_ENCRYPTION_KEY encrypts stored credentials and cannot be rotated without re-entering every credential. WEBHOOK_URL decides what URL n8n hands to Slack or GitHub as a callback; get it wrong and OAuth apps redirect to localhost. N8N_HOST affects generated links inside the UI.

Which proxy should sit in front?

Caddy, if you want two lines and automatic certificates. Nginx, if you already run it and want the standard snippet.

Caddy config:

n8n.example.com {
  reverse_proxy n8n:5678
}

Nginx needs the usual proxy_set_header Host, X-Real-IP, X-Forwarded-For, X-Forwarded-Proto lines plus a client_max_body_size 100m; directive, because the default 1 MB limit breaks file uploads in the Google Drive and S3 nodes.

Webhooks only work over HTTPS in practice. Slack, Stripe and GitHub all reject plain HTTP callback URLs.

When do you need queue mode?

Queue mode activates when a single main process cannot keep up with concurrent executions — typically past 2,000 executions a day on a 2 GB instance, or whenever a single workflow runs heavy AI nodes that take 30+ seconds per item.

It adds Redis and a separate worker container:

EXECUTIONS_MODE: queue
QUEUE_BULL_REDIS_HOST: redis
QUEUE_HEALTH_CHECK_ACTIVE: true

Then run additional containers with command: worker --concurrency=5. Each worker handles 5 jobs at once. A €10 VPS with two workers comfortably runs around 15 concurrent workflows.

Warning: queue mode needs N8N_ENCRYPTION_KEY to be identical on main and every worker. Mismatched keys produce “credentials could not be decrypted” errors that look like data corruption.

How do you back this up properly?

Dump the database nightly and copy the n8n_data volume weekly. Workflows live in Postgres after the migration, so the volume mostly holds binary data and a legacy SQLite file you no longer use.

docker exec postgres pg_dump -U n8n n8n | gzip > /backups/n8n-$(date +%F).sql.gz

Keep 14 daily dumps. Restore is gunzip -c file.sql.gz | docker exec -i postgres psql -U n8n n8n. Test a restore once. An untested backup is a wish.

What still goes wrong after a clean install?

Timezones. Set GENERIC_TIMEZONE and TZ to the same value. A mismatch makes cron schedules fire hours off, and the schedule node shows the correct time while the server disagrees.

Also expect version upgrades to occasionally change node output shapes. Pin the image tag (n8nio/n8n:1.9x) instead of using latest, and bump deliberately after checking the release notes for breaking changes in nodes you depend on.

Who should not self-host?

Anyone without someone who can read a container log on a Sunday. Downtime means webhooks queue up at the provider and may expire. If a missed execution costs money, €20 a month for Cloud is cheaper than a pager.

Teams that need compliance evidence, SSO and environments will end up on Business (€800/month yearly) or Enterprise anyway, where self-hosting still works but support expectations shift.

Quick Summary

  • Switch n8n to Postgres with DB_TYPE=postgresdb and four DB_POSTGRESDB_* variables.
  • Run pg_dump nightly; workflows live in the database, not the volume.
  • Queue mode (Redis plus workers) is worth enabling past ~2,000 executions/day.
  • Pin the image tag and set GENERIC_TIMEZONE to avoid cron surprises.
  • Back up N8N_ENCRYPTION_KEY separately — without it, credentials are unrecoverable.
Tags: