n8n Self-Hosting Guide: Docker Compose, PostgreSQL, HTTPS, Backups and Monitoring
By Abhishek Patoliya · 2026-08-12
n8n runs either as n8n Cloud (managed, billed per workflow execution) or self-hosted on infrastructure you control. This first lesson of Learn n8n sets up a self-hosted instance the way n8n's documentation now recommends: Docker Compose with PostgreSQL, a persistent volume, an explicit encryption key, HTTPS in front, and the health checks and backups that make it safe to rely on.
Just want to try n8n on your laptop? The Run n8n locally with Docker series is the gentler path. This lesson is for an instance that other systems will depend on. Settings and defaults below were checked against the n8n documentation on 9 November 2026.
Cloud or self-hosted?
| n8n Cloud | Self-hosted (Community edition) |
| Cost | Plan price, billed per workflow execution | Free licence; you pay for the server and the time to run it |
| Who handles upgrades, backups, uptime | n8n | You |
| Where workflows, credentials and execution data live | n8n's infrastructure | Yours |
| Reaching internal systems (private databases, intranet APIs) | Only if exposed to the internet | Directly, from inside your network |
| Features | Per plan | Community edition excludes some features (for example SSO, environments, external secrets, Git version control), which need a paid licence |
Self-hosting is worth it when you need data control, private network access or predictable cost at high volume, and someone will own the operations described below. n8n's licence (the Sustainable Use License) allows self-hosting for your own internal business use; if you plan to host n8n for paying customers, read it first. The platform choice itself is covered in n8n vs Zapier vs Make.
What you need
- A Linux server or VM (a small VPS is enough to start) with Docker Engine and the Docker Compose plugin installed.
- A domain or subdomain pointing at it, for example
n8n.example.com.
- Basic command-line comfort: editing a file, running
docker compose.
Step 1: secrets in a .env file
Create a folder (for example ~/n8n) and a .env file in it. Generate the values once and never commit this file anywhere:
# .env (keep private; back it up somewhere safe, separately from the server)
POSTGRES_PASSWORD=replace-with-a-long-random-password
N8N_ENCRYPTION_KEY=replace-with-output-of: openssl rand -hex 32
The encryption key is the most important secret here. n8n uses it to encrypt every stored credential. If you don't set one, n8n generates a random key on first launch and saves it inside its data folder (/home/node/.n8n). Setting it explicitly means you control it and can back it up. If you ever lose it, every stored credential becomes unreadable. If you later run several n8n processes (queue mode), every one must use the same key.
Step 2: the Compose file
# docker-compose.yml
services:
postgres:
image: postgres:16
restart: unless-stopped
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: n8n
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
interval: 5s
timeout: 5s
retries: 10
n8n:
image: n8nio/n8n:2.x.y # replace with an exact current release; see "Upgrading"
restart: unless-stopped
ports:
- "127.0.0.1:5678:5678" # only reachable from the server; the proxy faces the internet
environment:
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_DATABASE: n8n
DB_POSTGRESDB_USER: n8n
DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
N8N_HOST: n8n.example.com
N8N_WEBHOOK_URL: https://n8n.example.com/
N8N_PROXY_HOPS: "1"
GENERIC_TIMEZONE: Europe/London
TZ: Europe/London
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
volumes:
postgres_data:
n8n_data:
What each part does:
- PostgreSQL instead of SQLite. n8n uses SQLite by default; the docs describe it as fine for trying things out and recommend Postgres for a production instance. The
DB_POSTGRESDB_* variables point n8n at the postgres service. (n8n's own Compose example uses a newer Postgres image; whichever major version you choose, check where that image expects its data volume, because it has changed between versions.)
- The
n8n_data volume at /home/node/.n8n. Even with Postgres and an explicit key, n8n's documentation recommends keeping this folder persistent.
N8N_WEBHOOK_URL is the public base URL that n8n shows for webhooks. Without it, n8n builds URLs from its internal host and port, which external services can't reach. It replaces the older WEBHOOK_URL, which is deprecated (see the webhooks lesson). N8N_PROXY_HOPS tells n8n there is one reverse proxy in front, so it sees real client IPs.
GENERIC_TIMEZONE is the zone schedule triggers use; it defaults to America/New_York, which surprises everyone outside New York. TZ sets the container's system time zone for scripts and commands. Set both to your business's zone (see dates and time zones).
- Port bound to
127.0.0.1, so n8n is only reachable through the reverse proxy.
Start it with docker compose up -d, then docker compose logs -f n8n until n8n reports that it is ready. Validate the file first with docker compose config, which prints the resolved configuration or the error.
Step 3: HTTPS in front
n8n can serve HTTPS itself (N8N_PROTOCOL, N8N_SSL_KEY, N8N_SSL_CERT), but most installations put a reverse proxy in front, which also renews certificates automatically. With Caddy installed on the server, the whole configuration is:
# /etc/caddy/Caddyfile
n8n.example.com {
reverse_proxy 127.0.0.1:5678
}
Caddy obtains and renews the certificate for that domain automatically. Nginx or Traefik work equally well with more configuration. Many webhook senders require HTTPS, and you shouldn't send credentials to the editor over plain HTTP anyway. n8n's secure-cookie setting (N8N_SECURE_COOKIE) defaults to true, which expects HTTPS.
Step 4: settings you don't need to change (and one you might)
- Execution data pruning is on by default:
EXECUTIONS_DATA_PRUNE=true, keeping executions for 336 hours (14 days) and at most 10,000. Lower these if your workflows store large AI request and response payloads; raise them only if you need longer history (and plan for the disk and privacy implications).
- Secure cookies are already on, as above.
- Metrics (
/metrics) are off unless you set N8N_METRICS=true; turn them on if you use Prometheus or similar.
Step 5: health checks and monitoring
A self-hosted instance has nobody else watching it. n8n's main process always exposes two endpoints:
/healthz returns 200 when the instance is reachable.
/healthz/readiness returns 200 once the database is connected and migrated.
# from a monitoring service or another machine, not the n8n server itself
curl -fsS https://n8n.example.com/healthz/readiness || echo "n8n not ready"
Point an external uptime monitor at the readiness URL, and add three more checks: disk usage on the server (Docker volumes and logs grow), a "heartbeat" workflow on a schedule that pings your monitor so you know schedules are actually firing, and an error workflow that alerts you when any workflow fails. Test the alert once by stopping the container in a quiet moment.
Step 6: backups you have actually restored
- Database: a scheduled
pg_dump, stored off the server: docker compose exec -T postgres pg_dump -U n8n n8n | gzip > n8n-$(date +%F).sql.gz
- The
.env file (encryption key and database password), stored separately from the database dumps. A dump without the key gives you workflows whose credentials can't be decrypted.
- Restore test: once, restore a dump into a fresh test instance with the same encryption key and confirm a workflow with credentials still runs. An untested backup is a hope, not a backup.
Upgrading
Pin an exact image version (never latest in production). To upgrade: read the release notes for breaking changes, take a fresh backup, change the version in docker-compose.yml, run docker compose pull && docker compose up -d, and check the logs and your heartbeat. Rehearse larger upgrades on a copy first. The upgrading safely lesson goes step by step.
What self-hosting doesn't solve
Self-hosting controls where n8n runs. It doesn't change what happens when a workflow sends data to an external LLM API: that data leaves your infrastructure under the model provider's terms. If privacy is the goal, also decide which data you send to models and to which provider; see handling PII in AI automation.
Key takeaways
- Use Docker Compose with PostgreSQL, a persistent
/home/node/.n8n volume and an explicit, backed-up encryption key.
- Set
N8N_WEBHOOK_URL, N8N_PROXY_HOPS and GENERIC_TIMEZONE; put HTTPS in front.
- Pruning and secure cookies are on by default; tune pruning for large AI payloads.
- Monitor
/healthz/readiness externally, back up the database and the key separately, and test a restore.
Exercise: bring the stack up, create a workflow with a Schedule Trigger that pings a free uptime monitor every five minutes, then take a pg_dump, delete the stack's volumes on a test server and restore. Next, start building workflows with the Webhook node.
References (checked 9 November 2026)
Written by Abhishek Patoliya, Senior n8n Developer & Automation Architect, Surat, India. Verified n8n Creator.