Self-Host n8n with Docker and PostgreSQL: Production Setup
A production n8n Docker PostgreSQL setup: Compose file, encryption key, volumes, task runners, backups, upgrades and the settings to change on day one.
Executive Summary: A production n8n deployment needs more than one container. It needs PostgreSQL instead of SQLite, a fixed encryption key, a correct public webhook URL, external task runners for code isolation, execution pruning and backups you have actually restored. This guide walks through a Docker Compose setup with all of those pieces, explains each decision and lists the mistakes that cause the most pain later. It is the base I build my own DevOps and alerting workflows on.
Running n8n with a single docker run command takes a minute. Running it so that it survives restarts, upgrades and a bad disk takes some planning. The difference shows up months later, usually when you try to restore credentials and discover the encryption key changed.
This guide sets up n8n Docker PostgreSQL deployment for a single server, which is where most self-hosted teams start. It covers the Compose file, the settings that matter and the operational habits that keep it healthy. If you are still deciding whether to self-host at all, read n8n Cloud vs self-hosted first.
The Target Architecture
The setup has four services on one host:
- n8n, the main application, serving the editor, API and webhooks.
- Task runners, a sidecar that executes Code node JavaScript and Python in isolation.
- PostgreSQL, storing workflows, credentials and execution history.
- A reverse proxy, terminating TLS and forwarding traffic to n8n.
This is the shape I use for my own instance, which runs my DevOps, CI/CD and error alerting workflows. It is simple enough for one person to operate, and it scales cleanly into queue mode later without a rebuild.
Why PostgreSQL Instead of SQLite
n8n defaults to SQLite, which is fine for evaluation. However, SQLite is a single file with limited concurrency. Under steady load it becomes a bottleneck, and queue mode does not support it at all. The official Docker Compose guide recommends PostgreSQL for any instance running real workloads.
Note that n8n 2.0 dropped MySQL and MariaDB support, which settles the database question for any new n8n Docker PostgreSQL deployment. In practice, an n8n Docker PostgreSQL stack is now the standard choice for production self-hosting, and the one the community tests against most.
The n8n Docker PostgreSQL Compose File
Below is a trimmed version of the Compose file. Secrets live in a .env file next to it, which never goes into version control.
services:
postgres:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: n8n
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d n8n"]
interval: 10s
retries: 5
n8n:
image: n8nio/n8n:${N8N_VERSION}
restart: unless-stopped
environment:
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_DATABASE: n8n
DB_POSTGRESDB_USER: ${POSTGRES_USER}
DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
N8N_HOST: n8n.example.com
N8N_PROTOCOL: https
WEBHOOK_URL: https://n8n.example.com/
N8N_PROXY_HOPS: 1
GENERIC_TIMEZONE: Asia/Kolkata
N8N_RUNNERS_MODE: external
N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
N8N_RUNNERS_AUTH_TOKEN: ${RUNNERS_TOKEN}
EXECUTIONS_DATA_PRUNE: "true"
EXECUTIONS_DATA_MAX_AGE: 336
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
runners:
image: n8nio/runners:${N8N_VERSION}
restart: unless-stopped
environment:
N8N_RUNNERS_TASK_BROKER_URI: http://n8n:5679
N8N_RUNNERS_AUTH_TOKEN: ${RUNNERS_TOKEN}
depends_on:
- n8n
volumes:
db_data:
n8n_data:
The reverse proxy sits in front of this, either as another service in the same file or on the host. I leave it out here because the choice between Caddy, Nginx and Traefik is a matter of taste. What matters is that it terminates TLS and passes the original host and protocol headers.
Reverse Proxy Essentials
Whichever proxy you choose, it has four jobs in an n8n Docker PostgreSQL stack. First, it terminates TLS with a valid certificate, ideally renewed automatically. Second, it forwards the original host and protocol headers, so n8n builds correct URLs. Third, it supports WebSocket upgrades, because the editor uses a persistent connection for live updates. Finally, it sets sensible limits on request size and timeouts.
Request size deserves a closer look. n8n accepts webhook payloads up to 16 MB by default, controlled by N8N_PAYLOAD_SIZE_MAX. If your proxy allows only 1 MB, large webhooks fail at the proxy with an error n8n never sees. Align the two limits, and the failure becomes visible in one place instead of two.
It is also worth restricting the editor. Webhooks must be public, but the editor and API rarely need to be. Many teams put the editor behind a VPN or an identity-aware proxy, and leave only the /webhook/ paths open to the internet. That single change removes most of the attack surface.
Settings That Matter Most
The encryption key
n8n encrypts stored credentials with N8N_ENCRYPTION_KEY. If you do not set it, n8n generates one and saves it in the data volume. That works until the volume is lost or recreated, at which point every credential becomes unreadable.
Therefore, generate the key yourself, set it explicitly and store a copy in your password manager or secrets store. Treat it like a database master key, because that is what it is. My post on secrets management for backend systems covers how to store keys like this safely.
The public webhook URL
Behind a reverse proxy, n8n cannot guess its public address. Without WEBHOOK_URL, the editor shows internal URLs like http://localhost:5678, and external services cannot reach your webhooks. Set it to the public HTTPS address, and set N8N_PROXY_HOPS so n8n trusts the forwarded headers from your proxy.
External task runners
Task runners became the default in n8n 2.0. They run Code node scripts in a separate process, so user code cannot reach n8n’s internals. In internal mode, that process is a child of n8n on the same host, which the documentation describes as insecure by design for production use.
External mode moves runners into their own container, as in the Compose file above. The n8nio/runners image version must match the n8n version exactly, so pin both with the same variable. The task runners documentation explains how to add extra packages by extending the runners image.
Execution pruning
Every production execution stores its data. Without pruning, the database grows until it fills the disk or slows every query. Pruning is on by default in current versions, keeping executions for 14 days (336 hours) and capping the total count. Even so, set the values explicitly so they are visible and deliberate.
For high-volume workflows, consider not saving successful executions at all. Failed executions are the ones you need for debugging; successful ones are often just noise.
Backups You Have Actually Restored
A backup you have never restored is a hope, not a backup. This applies to every n8n Docker PostgreSQL deployment, however small. For this setup, a complete backup has three parts:
- The database, using a scheduled
pg_dumpto storage off the host. - The encryption key, stored separately from the database dump.
- Workflow exports, using the n8n CLI to export workflows as JSON into version control.
The database dump restores everything, but only with the matching encryption key. The workflow exports give you readable history and a quick way to move a single workflow. Together, they cover both disasters and mistakes.
Test the restore on a separate host at least once per quarter, using the same n8n Docker PostgreSQL Compose file as production. Start a fresh stack, load the dump, set the key and confirm that credentials work. If any step surprises you, update the runbook before you need it for real. My guide to PostgreSQL performance tuning also helps once the execution tables grow.
Upgrades Without Surprises
n8n releases often. A minor release every week or so is normal, and major versions such as 2.0 bring breaking changes. A safe upgrade routine looks like this:
- Read the release notes for every version you are skipping.
- Take a database backup immediately before upgrading.
- Update
N8N_VERSIONonce, so n8n and runners move together. - Pull images, restart and watch the logs for migration errors.
- Run a smoke test: one webhook workflow and one scheduled workflow.
For my own instance, this routine runs through a small CI/CD pipeline. A change to the version variable triggers the deploy, and an n8n workflow posts the result to Telegram. If the health check fails after a restart, the alert says so before anyone notices a broken workflow.
Monitoring the Stack
A healthy n8n Docker PostgreSQL deployment still needs someone to notice when it stops being healthy. n8n exposes a /healthz endpoint that returns success when the process is up, which is enough for a basic uptime check. For deeper visibility, you can enable the Prometheus metrics endpoint with N8N_METRICS=true and scrape it from your existing monitoring.
Beyond the application, watch three things on the host: disk usage on the database volume, PostgreSQL connection count and memory pressure on the n8n container. Disk is the usual culprit. Execution data grows quietly, and a full disk stops PostgreSQL, which stops every workflow at once.
Finally, give the instance an error workflow from day one. When any production workflow fails, it sends the workflow name, the failing node and a link to the execution to a channel someone reads. On my instance, that channel is Telegram, and the same route carries deploy results and health check failures. One place for every alert keeps the signal easy to follow.
Common Mistakes
- No explicit encryption key. The most painful mistake, because it surfaces only during a restore.
- Using the
latesttag. An unplanned restart can pull a major version with breaking changes. - Mismatched runner version. Runners fail to connect when the versions differ.
- Exposing port 5678 directly. Always put a TLS-terminating proxy in front of the editor and webhooks.
- Backups on the same disk. A disk failure takes the database and the backup together.
Frequently Asked Questions
Can I use MySQL with n8n?
Not on current versions. n8n 2.0 removed MySQL and MariaDB support, so PostgreSQL is the production choice, with SQLite only for evaluation.
Do I need external task runners for a small instance?
If anyone other than you can edit workflows, or the instance holds sensitive credentials, yes. External mode is the recommended production setup, and it adds only one container.
How much server does n8n need?
A small server with two virtual CPUs and four gigabytes of memory handles many teams comfortably. Execution volume, large files and Code node usage drive the requirement more than the number of workflows.
A Foundation Worth Getting Right
Your n8n Docker PostgreSQL setup is the foundation every workflow stands on. Fix the encryption key, set the webhook URL, isolate code with external runners, prune executions and restore your backups on schedule. With those in place, scaling to queue mode later is an addition, not a migration.
Last updated on 29 November 2026