Self-Host n8n with Docker Compose, PostgreSQL and Automatic HTTPS (2026 Guide)
Short on time? The n8n Production Self-Hosting Kit ($29) is the finished, tested version of what this tutorial builds.
Running n8n yourself is cheap and gives you full control. The default "quick start" (SQLite, no HTTPS, latest tag) is fine for a test, but not for workflows your business depends on.
This is the setup we tested end to end with n8n 2.40.6.
What you need
- A Linux server with 2 GB of RAM. Most €5–10/month VPS plans are enough for a small team.
- Docker with the Compose plugin. Check with
docker compose version. - A subdomain such as
n8n.example.comwith an A record pointing at the server. - Ports 80 and 443 open. Caddy needs both to get the certificate.
(Prefer not to run a server? n8n Cloud is the hosted option.)
The three files
Generate them with the free n8n Docker Compose generator. It creates the database password and encryption key in your browser. Or write them by hand:
docker-compose.ymlhas three services:postgres:17-alpinewith a health checkdocker.n8n.io/n8nio/n8n:<exact version>, which waits for Postgres to be healthycaddy:2-alpine, which publishes 80/443 and proxies ton8n:5678.envholds the domain, email, timezone, version, database password andN8N_ENCRYPTION_KEY.Caddyfileis a few lines: your email, your domain andreverse_proxy n8n:5678. Caddy obtains and renews the Let's Encrypt certificate automatically.
Then:
chmod 600 .env
mkdir -p local-files
docker compose up -d
docker compose ps # wait until n8n shows (healthy)
Open https://n8n.example.com and create the owner account.
The n8n 2.x settings that matter
We found these while testing on 2.40.6:
- Use PostgreSQL 17 or newer. With Postgres 16, n8n 2.40 logs "Postgres 16 is outside the supported range and receives compatibility support only."
- The Read/Write Files node is sandboxed. n8n 2.x only allows folders listed in
N8N_RESTRICT_FILE_ACCESS_TO. Set it to/files, mount./local-files:/files, and create subfolders before writing to them. Otherwise you get "Access to the file is not allowed". - Set the URL variables behind a proxy. Set
N8N_HOST,N8N_PROTOCOL=https,WEBHOOK_URL=https://your-domain/andN8N_PROXY_HOPS=1. Without them, webhook URLs showlocalhost:5678.
Also worth setting:
EXECUTIONS_DATA_PRUNE=truewith a max age, so the database doesn't grow foreverGENERIC_TIMEZONE, so Schedule triggers fire at the times you expect
Pin the version
Use an exact tag like 2.40.6, never latest. To upgrade:
- Read the release notes.
- Take a backup.
- Change the version in
.env. - Run
docker compose pull n8n && docker compose up -d n8n.
Don't downgrade without restoring the pre-upgrade backup, because newer versions may migrate the database.
Save the encryption key now
Every credential you store in n8n is encrypted with N8N_ENCRYPTION_KEY. A database backup without the key cannot decrypt a single credential. Put the key in your password manager today. The next tutorial covers how to back up n8n properly.
Done-for-you version
The n8n Production Self-Hosting Kit is this setup plus a set of tested scripts:
setup.sh, which generates secrets and never overwrites an existing key- encrypted
backup.sh restore.sh, which refuses a mismatched key- a restore drill that proves your credentials decrypt
- a safe
upgrade.sh - a health check
We tested it by wiping every volume and restoring.
Not affiliated with n8n GmbH.
Free tool: n8n Docker Compose Generator (Postgres + HTTPS)
Written with AI assistance. Every formula, command and number on this page was tested before publishing (see each section). Product links go to our store.