n8n Docker Compose: a production setup

One Compose file that gets n8n right: Postgres instead of SQLite, named volumes that survive a rebuild, execution pruning so the database does not grow forever, and healthchecks so services start in the correct order.

Disclosure: contains an affiliate link to Inferno VPS.

Why not the one-line docker run?

The docker run n8nio/n8n command from the quickstart uses SQLite and an anonymous volume. It is fine for a five-minute test and wrong for anything you rely on: SQLite locks under concurrent executions, and an anonymous volume disappears the first time you recreate the container. Compose fixes both and makes the whole stack reproducible. If you have not done the base install yet, start with how to install n8n on a VPS.

The full Compose file

services:
  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: n8n
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n"]
      interval: 10s
      timeout: 5s
      retries: 5

  n8n:
    image: docker.n8n.io/n8nio/n8n:latest
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=${DB_PASSWORD}
      - N8N_HOST=n8n.example.com
      - N8N_PROTOCOL=https
      - WEBHOOK_URL=https://n8n.example.com/
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - GENERIC_TIMEZONE=Europe/Berlin
      - N8N_PROXY_HOPS=1
      - EXECUTIONS_DATA_PRUNE=true
      - EXECUTIONS_DATA_MAX_AGE=336
      - EXECUTIONS_DATA_PRUNE_MAX_COUNT=10000
      - N8N_DIAGNOSTICS_ENABLED=false
    volumes:
      - ./n8n-data:/home/node/.n8n

What each block does

Postgres with a bind-mounted volume

The database lives in ./pgdata next to the Compose file, so a docker compose down && up never loses data and backups are a simple directory or pg_dump away. The healthcheck plus depends_on: condition: service_healthy guarantees n8n does not try to connect before Postgres accepts connections — the single most common first-boot error.

Port bound to localhost

127.0.0.1:5678:5678 keeps n8n off the public internet. All traffic goes through Nginx, which terminates TLS — see Nginx reverse proxy + SSL. Never publish 0.0.0.0:5678 on a server with a public IP.

Execution pruning

Every workflow run is stored. On a busy instance that table becomes the biggest thing on disk within weeks. EXECUTIONS_DATA_PRUNE=true with a max age of 336 hours (14 days) and a 10,000-row cap keeps it bounded. Tune to taste, but do not leave pruning off on a small VPS.

The encryption key

N8N_ENCRYPTION_KEY encrypts all stored credentials. It must stay identical across restarts and restores. Keep it in .env and in your password manager. The full list of tunables is in the environment variables reference.

The .env file

cat > .env <<EOF
DB_PASSWORD=$(openssl rand -hex 24)
N8N_ENCRYPTION_KEY=$(openssl rand -hex 24)
EOF

Compose automatically reads .env in the same directory. Keep it out of version control (echo ".env" >> .gitignore).

Bring it up and verify

docker compose up -d
docker compose ps
docker compose logs -f n8n     # watch for "Editor is now accessible"
docker compose exec db psql -U n8n -c "\dt"   # tables exist

Updating n8n safely

Pin to a specific tag in production rather than latest so an update is a deliberate act:

# change the image tag, e.g. n8nio/n8n:1.62.1, then:
docker compose pull
docker compose up -d
docker compose logs -f n8n

Always back up the database before a major version bump. For automatic image updates on non-critical stacks, see Watchtower — but do not point it at a production n8n without testing.

Running this on the right plan?

Postgres plus n8n plus a couple of concurrent workflows is comfortable on 4 GB RAM. Inferno's NVMe plans keep Postgres snappy and start at $3.49/mo.

View Inferno plans →

What to do next


Author: Hanna Wagner — Backend dev, Frankfurt. Last tested: 2026-05-21 on n8n 1.62, Postgres 16, Docker 27, Ubuntu 24.04.