Choose the Scope Before Starting

This guide describes a single n8n application and PostgreSQL database. Queue mode adds Redis and workers and needs its own capacity and recovery design. A universal request-per-second threshold cannot decide when to change topology.

Install Docker Engine and Compose v2, confirm docker compose version works, and choose a release supported by your environment. The examples use an explicit N8N_VERSION variable: set it to a stable release after reading its release and upgrade notes. Allow storage for execution history, files and backups; monitor real workflows before deciding on a production server size.

n8n is available under a Sustainable Use License and commercial licenses. Internal automation, offering hosted access and embedding n8n in a customer product are different uses. Resolve that scope before building a service around it.

Create the Project and Configuration

Create an empty directory for the Compose project. Keep the .env file out of version control and readable only by the administrator. Generate a strong database password and a separate random encryption key; store a protected recovery copy of the key.

mkdir n8n-compose
cd n8n-compose
# Generate two different random values, one for each secret:
openssl rand -hex 32
openssl rand -hex 32

Create .env. Replace every REPLACE_ value before starting. Use a public domain only when you have configured HTTPS; the initial binding below is private to the server.

N8N_VERSION=REPLACE_WITH_SUPPORTED_STABLE_VERSION
POSTGRES_PASSWORD=REPLACE_WITH_RANDOM_DATABASE_PASSWORD
N8N_ENCRYPTION_KEY=REPLACE_WITH_SEPARATE_RANDOM_KEY
GENERIC_TIMEZONE=Etc/UTC

Create compose.yml:

services:
  postgres:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_DB: n8n
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
      interval: 10s
      timeout: 5s
      retries: 10
  n8n:
    image: docker.n8n.io/n8nio/n8n:${N8N_VERSION:?set N8N_VERSION}
    restart: unless-stopped
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_USER: n8n
      DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY:?set N8N_ENCRYPTION_KEY}
      N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"
      GENERIC_TIMEZONE: ${GENERIC_TIMEZONE:-Etc/UTC}
      TZ: ${GENERIC_TIMEZONE:-Etc/UTC}
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy
volumes:
  postgres_data:
  n8n_data:

The database is not published on a host port. The named volumes persist outside the container lifecycle. The n8n data volume remains useful even with PostgreSQL: retain configuration and any filesystem data used by your setup. PostgreSQL major-version upgrades require their own migration; changing its image tag is not a database upgrade procedure.

Download the Compose example · Download the environment example. Replace all placeholder values before use.

Start and Verify the Instance

docker compose config --quiet
docker compose pull
docker compose up -d
docker compose ps
docker compose logs --tail=100 n8n
curl --fail http://127.0.0.1:5678/healthz

A successful health response is an initial check. Open the editor from the local machine, or use a secure tunnel when the server is remote. Set up the owner account, create a small workflow and save it. Restart only the n8n service, then confirm the workflow remains available. Test a real credential without placing its secret in screenshots or public logs.

The localhost binding is intentional. Do not change it to a public interface just to make a remote browser connect; configure HTTPS and access controls first.

Configure HTTPS and Webhooks

For external use, put the application behind a TLS reverse proxy and point your domain to the server. Set N8N_HOST, N8N_PROTOCOL=https and WEBHOOK_URL to the actual public address. When exactly one trusted reverse proxy is in front of n8n, set N8N_PROXY_HOPS=1; match this to your topology rather than copying it across a longer proxy chain.

Forward the host, protocol and client-address headers according to n8n and proxy guidance. Test both the editor and an activated workflow's production webhook. A test webhook endpoint and a production webhook endpoint are different URLs.

Back Up the Database and the Key Together

Create a logical database dump and protect it alongside the encryption key and configuration. This command creates a custom-format dump on the host:

docker compose exec -T postgres pg_dump -U n8n -d n8n -Fc > n8n.dump

Also back up any binary-file storage and the relevant n8n volume using a consistent procedure for your storage mode. Keep the same encryption key when recovering encrypted credentials. A workflow JSON export alone is not a complete credential, database or file backup.

Restore into a separate, isolated installation with the matching application release and database compatibility. Check a saved workflow and credential before switching traffic. A logical pg_dump is not continuous point-in-time recovery: PostgreSQL PITR requires a suitable base backup and archived WAL.

Troubleshoot the Specific Failure

SymptomFirst check
Image cannot be pulledReplace the version placeholder and verify the published release tag.
Database connection failsCheck service health, database/user names and matching password variables.
Credentials fail after restorationVerify that the restored installation uses the original encryption key.
Webhooks point at localhost or HTTPCheck the public webhook URL, proxy headers and trusted proxy count.
Memory grows during executionInspect payload size, binary data, node behavior, concurrency and execution retention.

Before an upgrade, retain a recovery set and the previous application version, read the release notes, and repeat the workflow/credential/webhook checks. Database schema changes can make an application-only rollback insufficient.

Official sources and review notes

Product documentation reviewed November 15, 2026. Selection advice is editorial; examples and decision maps are illustrative.