composebuilder

Docker Compose: n8n Workflow Automation with PostgreSQL

Self-host n8n 2.48 with PostgreSQL 16 in Docker Compose: encrypted credentials, database healthcheck, loopback-only port and a production checklist.

When to use this stack

n8n is a workflow automation tool. You connect triggers (webhooks, schedules, forms, email) to actions in hundreds of services, and it stores each workflow, each execution and each credential in a database. The default SQLite file is fine for a short test on a laptop. Once n8n runs 24/7 on a server, you usually want PostgreSQL instead, because it handles concurrent writes better, can be dumped and restored with standard tools, and keeps data separate from the application container. This stack pins n8n 2.48.0 and PostgreSQL 16, makes n8n wait until the database answers its readiness check, and stores the n8n data directory in a named volume so the encryption key file and settings survive container upgrades.

Use this stack when you run automations on a VPS, need webhooks from external services, or want database backups you can restore. Do not use it when you only need a few flows on your own computer, because the SQLite setup is simpler and has fewer moving parts. Do not use it either when you expect very high execution volume on a single container; for that, use queue mode with Redis and worker containers, described in the Variations section. Finally, do not run it without HTTPS in production. Webhooks and OAuth callbacks from external services need a public HTTPS address, and the editor sends session cookies that should never travel in clear text.

docker-compose.yml

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

  n8n:
    image: n8nio/n8n:2.48.0
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: ${POSTGRES_DB}
      DB_POSTGRESDB_USER: ${POSTGRES_USER}
      DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_HOST: ${N8N_HOST}
      N8N_PORT: 5678
      N8N_PROTOCOL: ${N8N_PROTOCOL}
      WEBHOOK_URL: ${WEBHOOK_URL}
      GENERIC_TIMEZONE: ${GENERIC_TIMEZONE}
      TZ: ${GENERIC_TIMEZONE}
    ports:
      - "127.0.0.1:5678:5678"
    volumes:
      - n8n_data:/home/node/.n8n

volumes:
  postgres_data:
  n8n_data:

.env

.env
POSTGRES_USER=n8n
POSTGRES_PASSWORD=CHANGE_ME_STRONG_DB_PASSWORD
POSTGRES_DB=n8n
N8N_ENCRYPTION_KEY=CHANGE_ME_64_HEX_CHARS_FROM_OPENSSL_RAND_HEX_32
N8N_HOST=n8n.example.com
N8N_PROTOCOL=https
WEBHOOK_URL=https://n8n.example.com/
GENERIC_TIMEZONE=Europe/Madrid

Start and verify

Shell
# 1. Generate the encryption key once and paste it into N8N_ENCRYPTION_KEY
openssl rand -hex 32

# 2. Validate the compose file and the variable substitution (no containers started)
docker compose config --quiet

# 3. Start both services in the background
docker compose up -d

# 4. PostgreSQL should show "(healthy)" and n8n should show "Up"
docker compose ps

# 5. Follow the n8n log until it reports that the editor is accessible
docker compose logs -f n8n

# 6. Health endpoint through the loopback port (expected: {"status":"ok"})
curl -fsS http://127.0.0.1:5678/healthz

Open the editor through your reverse proxy and create the owner account on the first visit. Anyone who reaches the setup screen first becomes the owner, so do this immediately after the first start.

Production checklist

  • Generate N8N_ENCRYPTION_KEY once, store it in a password manager and keep a copy next to your database backups. Changing it later breaks every stored credential.
  • Keep port 5678 bound to 127.0.0.1 and publish n8n only through a reverse proxy with HTTPS, such as Caddy, nginx or Traefik.
  • Keep N8N_PROTOCOL=https and WEBHOOK_URL equal to the public address, so the webhook URLs shown in the editor are correct.
  • Use a long, unique POSTGRES_PASSWORD and never publish port 5432 to the host.
  • Dump the database every day with pg_dump and copy the n8n_data volume. Run a test restore at least once.
  • Create the owner account at first start and invite other users only when they need access.
  • Keep the image tag pinned and upgrade on purpose after reading the release notes, because major n8n versions can change nodes and settings.
  • Limit who can reach the Docker host over SSH, because anyone with Docker access can read the encryption key from the volume.

Common problems

  • Symptom: n8n exits at start with a connection refused or password authentication failed error for PostgreSQL. Cause: the database volume was created earlier with other credentials, or the service started before PostgreSQL accepted connections. Fix: make the DB_POSTGRESDB_* values match POSTGRES_* in .env. If you changed POSTGRES_PASSWORD after the first start, change the password inside PostgreSQL too, because the volume keeps the old one.
  • Symptom: saved credentials show a decryption error after a restore. Cause: the restored N8N_ENCRYPTION_KEY is different from the key used when the credentials were created. Fix: put the original key back in .env and restart n8n.
  • Symptom: webhook URLs in the editor start with http://localhost:5678. Cause: WEBHOOK_URL and N8N_PROTOCOL do not describe the public address. Fix: set both values in .env and run docker compose up -d again.
  • Symptom: the domain loads a proxy error page while docker compose ps shows n8n as up. Cause: the reverse proxy points to the wrong port, or the port is bound to 127.0.0.1 while the proxy runs in another container. Fix: proxy to 127.0.0.1:5678 on the host, or place the proxy on the same Docker network and point it to the n8n service.
  • Symptom: n8n logs show permission denied under /home/node/.n8n. Cause: the folder was replaced by a bind mount owned by another user. Fix: use the named volume from this file, or change the owner of the host folder to UID 1000.

Variations

  • SQLite, single user: remove the postgres service, the depends_on block and every DB_* variable. n8n then stores its data in the n8n_data volume. This is the simplest setup for a personal instance.
  • Queue mode: add a Redis service and run one n8n main container plus one or more n8n worker containers. Set EXECUTIONS_MODE=queue and the Redis connection variables described in the n8n documentation. Use this when executions wait in line on a single container.
  • Caddy as reverse proxy: add a Caddy container that proxies your domain to n8n:5678 on the Docker network. Caddy obtains and renews the TLS certificate automatically, which removes manual certificate work.

Questions

Why does n8n need N8N_ENCRYPTION_KEY?

n8n encrypts every saved credential (API keys, OAuth tokens, passwords) with this key before writing it to the database. If the key is lost or changed, the stored credentials cannot be decrypted, so back it up together with the database.

Can I use SQLite instead of PostgreSQL?

Yes. SQLite is the n8n default and works for one user on one machine. PostgreSQL is the better choice for a server with backups, several users or a larger number of executions.

Why is the n8n port bound to 127.0.0.1?

Binding to the loopback interface keeps the editor unreachable from the internet until you place a reverse proxy with HTTPS in front of it. Webhooks from external services then reach n8n through that proxy.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Paperless-ngx with PostgreSQL and Redis

    Run Paperless-ngx 2.20 document archive with PostgreSQL 16 and Redis in Docker Compose. OCR language setup, loopback-only port, backups and a consume folder.

    • postgres
    • redis
    • paperless
    Difficulty: Intermediate
  • Home Assistant Smart Home Hub

    Run Home Assistant 2026.10 in Docker Compose with a pinned image, persistent config volume, timezone setup and reverse proxy notes for secure remote access.

    • homeassistant
    Difficulty: Intermediate
  • Nextcloud + MariaDB + Redis File Sync

    Self-host Nextcloud with MariaDB 11.4 and Redis in Docker Compose. Pinned tags, database healthcheck, memory cache and the production steps for HTTPS and cron.

    • nextcloud
    • db
    • redis
    Difficulty: Intermediate
  • Ollama + Open WebUI Local AI Chat

    Run local AI models with Ollama and a chat interface with Open WebUI in Docker Compose. Pinned images, a health check, loopback-only access and RAM guidance.

    • ollama
    • open-webui
    Difficulty: Beginner

Browse more Automation, AI & Self-Hosted stacks or the full library.