Mad World #Save_GAZA: Free Palestine  

Search

Run n8n with Docker Compose: PostgreSQL, Task Runners, and HTTPS

Run n8n with Docker Compose: PostgreSQL, Task Runners, and HTTPS

n8n runs using the n8nio/n8n image, exposed on port 5678, and by default stores workflows in SQLite. Mount /home/node/.n8n to persist the encryption key; with PostgreSQL configured, the database holds workflows and credentials, while the volume holds the key. Supply N8N_ENCRYPTION_KEY before first launch and back it up—without it, restored credentials cannot decrypt. Start locally to verify persistence, then add HTTPS and a public webhook URL for production.

Prepare a Linux host and choose the n8n docker image

Docker Engine and Docker Compose v2 are required. Check your installation:

Terminal
docker --version
docker compose version

The official n8n Docker image is docker.io/n8nio/n8n. The external task-runner image is ghcr.io/n8n-io/runners. Pin both to the same n8n version. For production deployments, use PostgreSQL instead of the default SQLite database. With SQLite, stop n8n before copying the .n8n folder; copying the database file while n8n is writing to it can produce an inconsistent backup.

This guide shows a local Docker Compose deployment on Linux, persistence verification, and the steps to add PostgreSQL, task runners, and HTTPS using Traefik. It does not cover NAS-specific paths, reverse-proxy configurations beyond the Traefik example, or platform-specific firewall rules. Self-hosting n8n requires knowledge of Docker, networking, databases, and security configuration.

Create a Compose deployment with persistent data

Create the project directory and subdirectories for secrets and backups:

Terminal
mkdir -p ~/n8n-production/{secrets,backups}
cd ~/n8n-production
chmod 700 secrets backups

Generate the encryption key, PostgreSQL password, and task runner authentication token:

Terminal
openssl rand -hex 32 > secrets/n8n_encryption_key
openssl rand -hex 32 > secrets/postgres_password
openssl rand -hex 32 > secrets/runner_auth_token
chmod 600 secrets/*

n8n creates a random encryption key automatically on first launch and saves it in the ~/.n8n folder. It uses that key to encrypt credentials before saving them to the database. If the key is lost, a restored database cannot decrypt credentials. Generate your own key before first launch and store it in the secrets/ directory alongside the Compose file. Back up this directory securely; do not commit secrets to version control.

Create .env:

Code
# Pin to an exact stable release from https://github.com/n8n-io/n8n/releases
N8N_VERSION=2.43.4

POSTGRES_USER=n8n
POSTGRES_DB=n8n

# Public hostname where n8n will be reachable (update later for HTTPS)
N8N_HOST=localhost

# Timezone for schedule triggers
GENERIC_TIMEZONE=America/New_York

Read the encryption key and runner authentication token into .env:

Terminal
echo "N8N_ENCRYPTION_KEY=$(cat secrets/n8n_encryption_key)" >> .env
echo "N8N_RUNNERS_AUTH_TOKEN=$(cat secrets/runner_auth_token)" >> .env
chmod 600 .env

If you keep this folder in a Git repository, add .env and secrets/ to .gitignore. A key that was already committed stays in the history: our guide shows how to find leaked keys in Git history with Gitleaks and remove them.

Create compose.yaml:

Code
services:
  postgres:
    image: postgres:18
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
      PGDATA: /var/lib/postgresql/data
    volumes:
      - postgres_data:/var/lib/postgresql/data
    secrets:
      - postgres_password
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"
        ]
      interval: 5s
      timeout: 5s
      retries: 10

  n8n:
    image: docker.io/n8nio/n8n:${N8N_VERSION}
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "127.0.0.1:5678:5678"
    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_FILE: /run/secrets/postgres_password
      
      N8N_HOST: ${N8N_HOST}
      N8N_PORT: "5678"
      
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"
      N8N_BLOCK_ENV_ACCESS_IN_NODE: "true"
      N8N_BLOCK_FILE_ACCESS_TO_N8N_FILES: "true"
      
      GENERIC_TIMEZONE: ${GENERIC_TIMEZONE}
      TZ: ${GENERIC_TIMEZONE}
      
      N8N_RUNNERS_MODE: external
      N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
      N8N_RUNNERS_AUTH_TOKEN: ${N8N_RUNNERS_AUTH_TOKEN}
      
      NODE_ENV: production
    volumes:
      - n8n_data:/home/node/.n8n
    secrets:
      - postgres_password
    security_opt:
      - no-new-privileges:true

  task-runners:
    image: ghcr.io/n8n-io/runners:${N8N_VERSION}
    restart: unless-stopped
    depends_on:
      - n8n
    environment:
      N8N_RUNNERS_TASK_BROKER_URI: http://n8n:5679
      N8N_RUNNERS_AUTH_TOKEN: ${N8N_RUNNERS_AUTH_TOKEN}
      N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT: "15"
    security_opt:
      - no-new-privileges:true

volumes:
  n8n_data:
  postgres_data:

secrets:
  postgres_password:
    file: ./secrets/postgres_password

PostgreSQL 18 changed its default data directory layout. The PGDATA environment variable keeps the data in the same location as earlier versions so the volume mount remains predictable. Docker Compose secrets mount files at /run/secrets/<secret_name> inside containers; services must declare which secrets they need. n8n supports file-based configuration for database credentials by adding _FILE to the variable name. Secrets are supported on Linux containers only.

External task runners isolate Code node execution from the main n8n process. The n8n_data volume holds the encryption key and, when using the default SQLite database, the database file itself; with PostgreSQL, workflows and credentials live in the PostgreSQL volume, and n8n_data holds the key.

Validate the Compose file and start the services:

Terminal
docker compose config --quiet
docker compose pull
docker compose up -d

Check service status:

Terminal
docker compose ps

PostgreSQL should show healthy, n8n and task-runners should show running. Check n8n logs:

Terminal
docker compose logs n8n

Verify task runners connected:

Terminal
docker compose logs task-runners

Open http://localhost:5678 in your browser and complete the owner account setup. Create a simple workflow by clicking Add workflow, adding a node, clicking Save, then clicking the slider at the top right to set the workflow to Active. Stop and restart the stack:

Terminal
docker compose down
docker compose up -d

Refresh the browser. If your workflow is still present and active, the workflow database record persisted in PostgreSQL. This step verifies that n8n wrote to the database and can read from it after a restart, but does not independently verify the n8n_data volume or encryption key persistence without examining credentials.

Configure public access and webhook URLs

The documented example uses Traefik to terminate TLS and renew certificates. Before adding the Traefik service, create a DNS A record pointing your domain to the server's public IP address, and open ports 80 and 443 on your firewall.

Edit compose.yaml and insert the Traefik service at the top of the services: block, immediately after the services: line and before postgres::

YAML
  traefik:
    image: traefik
    restart: unless-stopped
    command:
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.web.http.redirections.entryPoint.to=websecure"
      - "--entrypoints.web.http.redirections.entrypoint.scheme=https"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
      - "--certificatesresolvers.mytlschallenge.acme.email=YOUR_EMAIL@example.com"
      - "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - traefik_data:/letsencrypt
      - /var/run/docker.sock:/var/run/docker.sock:ro

Replace YOUR_EMAIL@example.com with your actual email address for Let's Encrypt notifications. Add the traefik_data: volume under the volumes: section:

YAML
volumes:
  n8n_data:
  postgres_data:
  traefik_data:

Update the n8n service to remove the ports: block and add expose:, Traefik labels, and HTTPS environment variables. Replace the existing n8n service environment block with:

Code
    expose:
      - "5678"
    labels:
      - traefik.enable=true
      - traefik.http.routers.n8n.rule=Host(`${N8N_HOST}`)
      - traefik.http.routers.n8n.tls=true
      - traefik.http.routers.n8n.entrypoints=web,websecure
      - traefik.http.routers.n8n.tls.certresolver=mytlschallenge
    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_FILE: /run/secrets/postgres_password
      
      N8N_HOST: ${N8N_HOST}
      N8N_PORT: "5678"
      N8N_PROTOCOL: https
      N8N_WEBHOOK_URL: https://${N8N_HOST}/
      N8N_PROXY_HOPS: "1"
      
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"
      N8N_BLOCK_ENV_ACCESS_IN_NODE: "true"
      N8N_BLOCK_FILE_ACCESS_TO_N8N_FILES: "true"
      
      GENERIC_TIMEZONE: ${GENERIC_TIMEZONE}
      TZ: ${GENERIC_TIMEZONE}
      
      N8N_RUNNERS_MODE: external
      N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
      N8N_RUNNERS_AUTH_TOKEN: ${N8N_RUNNERS_AUTH_TOKEN}
      
      NODE_ENV: production

N8N_WEBHOOK_URL is available from n8n 2.30.0; on earlier versions, use WEBHOOK_URL.

Update .env to use your actual domain:

.env
N8N_HOST=n8n.example.com

Restart the stack:

Terminal
docker compose up -d

Check that Traefik, PostgreSQL, n8n, and task-runners all show running and that PostgreSQL shows healthy. Open https://n8n.example.com. The editor should load over HTTPS. Open a workflow, add a Webhook node, and check that the displayed webhook URL uses your domain with HTTPS. This confirms n8n is generating correct public URLs; it does not verify that an external service can reach your webhook. External webhook connectivity depends on your DNS, firewall, and network configuration, which this article does not test.

Back up n8n data consistently

A complete backup includes the PostgreSQL database, the n8n_data volume, the secrets/ directory, and your .env file. If you configured external storage for binary data or custom node directories, back those up as well.

Back up the PostgreSQL database to the backups/ directory:

Terminal
docker compose exec postgres pg_dumpall -U n8n > backups/n8n-$(date +%Y%m%d).sql

Back up the n8n_data volume by copying its contents from a temporary container:

Terminal
docker run --rm -v n8n-production_n8n_data:/data -v $(pwd)/backups:/backup alpine tar czf /backup/n8n_data-$(date +%Y%m%d).tar.gz -C /data .

Adjust the volume name prefix if your project directory name differs from n8n-production. Copy the secrets/ directory and .env file to your backup storage. Store backups in a separate, encrypted location. A restore needs the encryption key from either the backed-up secrets/n8n_encryption_key file or the N8N_ENCRYPTION_KEY line in the backed-up .env; both hold the same value.

Before updating, read the release notes at https://github.com/n8n-io/n8n/releases and check for breaking changes. Take a full backup using the commands above.

Update the Compose deployment

n8n Docker Compose update workflow: pull images, stop containers, start with new version

Update N8N_VERSION in .env to the new version, then pull and recreate the containers:

Terminal
docker compose pull
docker compose down
docker compose up -d

Check the logs:

Terminal
docker compose logs n8n

n8n runs database migrations automatically on startup. Open the editor and verify workflows load correctly. A documented rollback procedure after database migrations is not supported by the evidence reviewed for this article; reverting an image after schema changes requires a database restore from backup. Keep the pre-update database dump until you've confirmed the update succeeded.

Troubleshoot common problems

PostgreSQL container exits with "database files are incompatible with server"

The volume contains data from a different PostgreSQL major version. PostgreSQL 18 cannot open a data directory written by an older major version. If you have a pre-upgrade pg_dumpall backup, create a fresh volume, start the new PostgreSQL container, and restore the dump. If you do not have a dump, start a container using the previous PostgreSQL major version that matches your data, export with pg_dumpall, then follow the official PostgreSQL upgrade guide.

n8n shows localhost URLs instead of the public domain

N8N_WEBHOOK_URL, N8N_HOST, or N8N_PROTOCOL are missing or incorrect in the n8n service environment block. Verify the values in compose.yaml.

Task runners fail to connect

Verify N8N_RUNNERS_BROKER_LISTEN_ADDRESS is 0.0.0.0 in the n8n service and that the authentication token matches in both the n8n and task-runners services.

Port 5678 is already in use

Another process is using port 5678. Stop the other process or change the published port in the n8n service ports: line to 127.0.0.1:5679:5678.

Traefik cannot reach n8n

Verify both Traefik and n8n are connected to the same Docker network. Check the network configuration in compose.yaml and confirm with docker network inspect <network_name>.

Frequently asked questions

Can I run n8n without PostgreSQL?

Yes, n8n uses SQLite by default. Remove the postgres service, remove the depends_on block from the n8n service, remove the DB_TYPE and DB_POSTGRESDB_* environment variables, and remove the postgres_password secret. The n8n_data volume will hold both the database file and encryption key. Stop n8n before backing up the volume to avoid an inconsistent database file.

Do I need external task runners?

Task runners isolate Code node execution from the main n8n process. If you don't use the Code node or n8n AI, you can remove the task-runners service and the N8N_RUNNERS_* environment variables from the n8n service.

What happens if I lose the encryption key?

Restored workflows will load, but all saved credentials will be unreadable. You'll need to re-enter every credential. Back up the secrets/ directory and .env file to prevent this.

Can I use a different reverse proxy instead of Traefik?

Yes. Remove the Traefik service, keep ports: - "127.0.0.1:5678:5678" on the n8n service, configure your proxy to forward requests to localhost:5678, and set N8N_PROTOCOL=https, N8N_HOST, and N8N_WEBHOOK_URL to match your proxy's public URL.

Next step

Once n8n is running, explore workflow automation by following the official Quickstarts. For production scaling, see the n8n documentation on queue mode. For Docker fundamentals, read Docker for Beginners: Everything You Need to Know. For Compose concepts, read Docker Compose: What Is It and What Are Its Benefits?.

To get an alert when n8n stops answering, monitor it with Uptime Kuma on Docker.

Verification status: Supported by official documentation only under the conditions below; not reproduced in a test environment by IGPROD. Checked 2026-10-19.

Applies to: n8n 2.30.0.

Conditions:

  • The documented domain-based Compose procedure applies to a Linux server with Docker and Docker Compose installed.
  • Online access requires a subdomain pointed at the server and suitable DNS and firewall settings.
  • Use N8N_WEBHOOK_URL from n8n 2.30.0; use WEBHOOK_URL on earlier versions.
  • For SQLite, stop n8n before copying its data folder unless using a consistent snapshot.
  • A complete PostgreSQL backup also requires the external database and the .n8n folder.

Sources:

Yassine

Yassine runs IGPROD and has written about Synology NAS, Docker and self-hosting here since 2022. About IGPROD

Leave a comment

Your email address will not be published. Required fields are marked *

Your experience on this site will be improved by allowing cookies Cookie Policy