Self-hosted n8n with Docker Compose

A recipe you can run on a small EU server: n8n, PostgreSQL, an isolated task runner and a reverse proxy with automatic HTTPS. Plus the settings we were missing ourselves when the execution history suddenly took up 417 MB.

Last checked:

Step by stepHistory pruning – what we learned the hard way
  1. Settings
  2. Keep EXECUTIONS_DATA_SAVE_ON_SUCCESS=all globally, so you can debug everything else
  3. Raise EXECUTIONS_DATA_PRUNE_MAX_COUNT when you are on PostgreSQL. On SQLite you also have to run VACUUM to get the space back

Before you start: SQLite or PostgreSQL?

n8n uses SQLite unless you choose something else. That is fine for testing and small setups, but n8n recommends PostgreSQL for production, and SQLite does not work at all in queue mode.

We run two instances on SQLite in Docker Compose ourselves. It works, but it has cost us. A single flow that ran every 50 seconds pushed the database to 547 MB, 417 MB of which was execution history. A SQLite file also does not give space back by itself when old executions are deleted. That takes a VACUUM. If you are starting today, choose PostgreSQL from the beginning. Moving later means exporting and importing workflows and credentials.

The server: 2 CPUs and 4 GB of RAM are enough for n8n, PostgreSQL and the task runner. At Hetzner that is a CPX22 at €19.99 a month excluding VAT.

The files

Put three files in the same folder on the server: .env, docker-compose.yml and Caddyfile. We built this on n8n’s official example with PostgreSQL and added a reverse proxy, the time zone and history pruning.

# .env  – permissions 600, never in git
N8N_VERSION=2.48.6            # pinned version, never "latest"
N8N_DOMAIN=n8n.example.dk
POSTGRES_USER=postgres
POSTGRES_PASSWORD=skift-mig-til-noget-langt
POSTGRES_DB=n8n
POSTGRES_NON_ROOT_USER=n8n
POSTGRES_NON_ROOT_PASSWORD=skift-ogsaa-mig
# openssl rand -hex 32  (both of them)
N8N_ENCRYPTION_KEY=
RUNNERS_AUTH_TOKEN=
# docker-compose.yml
services:
  postgres:
    image: postgres:18
    restart: always
    environment:
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_DB
      - POSTGRES_NON_ROOT_USER
      - POSTGRES_NON_ROOT_PASSWORD
      - PGDATA=/var/lib/postgresql/data
    volumes:
      - db_storage:/var/lib/postgresql/data
      - ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 10

  n8n:
    image: docker.n8n.io/n8nio/n8n:${N8N_VERSION}
    restart: always
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - N8N_HOST=${N8N_DOMAIN}
      - N8N_PROTOCOL=https
      - N8N_EDITOR_BASE_URL=https://${N8N_DOMAIN}/
      - N8N_WEBHOOK_URL=https://${N8N_DOMAIN}/   # before 2.30: WEBHOOK_URL
      - N8N_PROXY_HOPS=1
      - GENERIC_TIMEZONE=Europe/Copenhagen
      - TZ=Europe/Copenhagen
      # task runner in its own container (external mode)
      - N8N_RUNNERS_MODE=external
      - N8N_RUNNERS_AUTH_TOKEN=${RUNNERS_AUTH_TOKEN}
      - N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0
      # history: keep errors, prune after 14 days or 50,000 executions
      - EXECUTIONS_DATA_SAVE_ON_ERROR=all
      - EXECUTIONS_DATA_SAVE_ON_SUCCESS=all
      - EXECUTIONS_DATA_SAVE_MANUAL_EXECUTIONS=false
      - EXECUTIONS_DATA_PRUNE=true
      - EXECUTIONS_DATA_MAX_AGE=336
      - EXECUTIONS_DATA_PRUNE_MAX_COUNT=50000
      # nodes that run code outside the task runner
      - NODES_EXCLUDE=["n8n-nodes-base.function","n8n-nodes-base.functionItem","@n8n/n8n-nodes-langchain.code"]
    volumes:
      - n8n_storage:/home/node/.n8n
      - ./backups:/backups
    depends_on:
      postgres:
        condition: service_healthy

  n8n-runner:
    image: n8nio/runners:${N8N_VERSION}
    restart: always
    environment:
      - N8N_RUNNERS_AUTH_TOKEN=${RUNNERS_AUTH_TOKEN}
      - N8N_RUNNERS_TASK_BROKER_URI=http://n8n:5679
    depends_on:
      - n8n

  caddy:
    image: caddy:2
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment:
      - N8N_DOMAIN
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data

volumes:
  db_storage:
  n8n_storage:
  caddy_data:
# Caddyfile – fetches a TLS certificate by itself
{$N8N_DOMAIN} {
    encode zstd gzip
    reverse_proxy n8n:5678
}

init-data.sh is the small script from n8n’s own example on GitHub (n8n-hosting) that creates a database user without superuser rights. Note that n8n itself has no port open to the internet. Only Caddy answers from outside, and that is also n8n’s own recommendation. N8N_WEBHOOK_URL exists from version 2.30. On older versions it is called WEBHOOK_URL.

History pruning – what we learned the hard way

n8n prunes by default: executions older than 336 hours, or beyond 10,000 in total, are deleted. That sounds sensible, but the limit is shared by all workflows. In our case, one flow that ran every 50 seconds used up the whole quota of 10,000, so every other flow only had about three days of history. The fix was to switch off saving successful executions for that one flow.

Do it like this:

  1. Open the frequent workflow → Settings → Save successful production executions: Do not save. Errors are still saved.
  2. Keep EXECUTIONS_DATA_SAVE_ON_SUCCESS=all globally, so you can debug everything else.
  3. Raise EXECUTIONS_DATA_PRUNE_MAX_COUNT when you are on PostgreSQL. On SQLite you also have to run VACUUM to get the space back.

One caveat: executions that are not saved do not show up in the executions list either. So monitor a frequent flow on its result, not only on its history.

Backup you can actually restore

An n8n backup has two parts: the database and the .n8n folder, which holds the encryption key. Without the key, credentials cannot be decrypted. We back up all workflows every day and keep them for 30 days.

#!/usr/bin/env bash
# /opt/n8n/backup.sh – run daily from cron, e.g. 03:30
set -euo pipefail
cd /opt/n8n
set -a; . ./.env; set +a          # POSTGRES_USER and POSTGRES_DB
stamp=$(date +%F)
mkdir -p "backups/$stamp"
chown -R 1000:1000 backups        # the n8n container writes as the user node (uid 1000)

# 1) workflows and credentials (encrypted) as JSON, one file per workflow
docker compose exec -T n8n n8n export:workflow --backup --output="/backups/$stamp/workflows/"
docker compose exec -T n8n n8n export:credentials --backup --output="/backups/$stamp/credentials/"

# 2) the whole database (users, settings, history)
docker compose exec -T postgres pg_dump -U "$POSTGRES_USER" -Fc "$POSTGRES_DB" > "backups/$stamp/n8n.dump"

# 3) keep 30 days, then copy somewhere other than the server
find backups -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec rm -rf {} +

The encryption key lives in .env. Keep a copy somewhere safe away from the server. Try a restore on a test server before you trust the backup. The CLI export only contains workflows and credentials, not users, history or variables.

Updates with a regression test

n8n releases a new minor version most weeks. On our side, one instance follows the newest release, while the other is pinned to a specific version. After every update we run a set of regression tests against the most important flows.

# update: backup → new version → test → otherwise roll back
./backup.sh
sed -i 's/^N8N_VERSION=.*/N8N_VERSION=2.48.6/' .env
docker compose pull n8n n8n-runner
docker compose up -d
docker compose logs -f n8n --since 2m   # look for migration errors

# Roll back: old version in .env + restore the database,
# because database migrations are not rolled back by themselves

The task runner must always have the same version as n8n. From n8n 3.0, Docker is the only supported way to self-host, and the internal task runner mode is deprecated. A complete update script with export, health check and rollback is on updating n8n.

Queue mode – only if you need it

Queue mode spreads executions across several workers via Redis. It requires PostgreSQL, Redis and the same encryption key on every worker. It gives you more to run and more things that can break. We do not run queue mode ourselves. Only consider it when a single instance cannot keep up, for example with many heavy executions at the same time. Long waiting times are more often caused by a single flow that runs far too often.

Sources

Last reviewed: 2026-12-03

Questions about self-hosted n8n

How big a server does n8n need?

For n8n with PostgreSQL and a task runner, 2 CPUs and 4 GB of RAM are a good starting point. With SQLite and a few flows, 2 GB can be enough. Flows with large files or many AI calls need more.

Why not just use the “latest” image tag?

Because a restart can then give you a new version you have not tested. Pin the version in .env, and update deliberately, with a backup first.

Can we move from SQLite to PostgreSQL?

Yes. Export workflows and credentials with n8n’s CLI, start a new instance with PostgreSQL and the same encryption key, and import them. History and users are not included in the CLI export.

Do we need queue mode?

Rarely. It requires Redis and PostgreSQL and gives you more to run. Start without it, and first fix the flows that run more often than they need to.

Fixed price, no commitment

Get a fixed price for your n8n task

Tell us briefly what you use today and what you want to achieve. A person answers by e-mail with a proposal and a fixed price before any work starts.

  • A fixed price before we start
  • An answer by e-mail, no calls
  • Your details are used only to answer you

Fields marked with * are required.

What is it about?