Self-host n8n with Docker and PostgreSQL
The cleanest way to self-host n8n with PostgreSQL is the withPostgres example from n8n’s own hosting repository: one PostgreSQL container, one n8n container, one task runner, and a volume for n8n’s .n8n folder. Pin the version, set your own encryption key and back it up, put n8n behind a reverse proxy with TLS, and expose only the webhook URLs that outside services must reach. The editor can stay on a private network.
At a glance
| Item | What this guide uses |
|---|---|
| n8n image | docker.n8n.io/n8nio/n8n:3.4.4 (also on Docker Hub as n8nio/n8n), the stable release on 5 December 2026 |
| Database | postgres:18, with PGDATA pinned as in n8n’s example |
| Schema | public by default; DB_POSTGRESDB_SCHEMA for a custom one |
| Data volume | /home/node/.n8n, which holds the encryption key |
| Port | 5678 bound to 127.0.0.1; nginx serves 443 |
| Public URLs | webhook and form paths only, via N8N_WEBHOOK_URL |
| Backups | PostgreSQL dump, the .n8n folder, your .env, optional CLI export |
Check versions again before you start. n8n says it releases a new minor version most weeks.
Is self-hosting the right choice?
n8n’s Docker install page says it recommends self-hosting for expert users and that mistakes can lead to data loss, security issues and downtime. If you have never kept a Linux server patched, n8n Cloud is the better choice: n8n handles hosting, updates and scaling. And if your “automation” is one scheduled job, a cron entry and a short script are easier to keep running than a workflow platform.
The licence matters too. n8n is distributed under the Sustainable Use License, which n8n calls “fair-code”, plus the n8n Enterprise License for files with .ee. in the name. n8n calls its code source-available and says it does not call itself open source, because the Open Source Initiative’s definition does not allow limits on use. The key limitation in LICENSE.md reads: “You may use or modify the software only for your own internal business purposes or for non-commercial or personal use.” n8n’s licence FAQ says personal projects, running your own business, and building automations for clients who cannot create or edit them are fine. Hosting n8n as a service where other people build their own workflows is not, and needs a commercial agreement. Enterprise features that ask for a licence key need a paid licence.
For comparison, n8n Cloud plans as displayed on n8n’s pricing page:
| Plan | Price shown (billed annually) | Workflow executions | Hosting |
|---|---|---|---|
| Starter | 20 EUR per month | 2.5K per month | Hosted by n8n |
| Pro | 50 EUR per month | 10K per month | Hosted by n8n |
| Business | 667 EUR per month | 40K per month | Self-hosted only |
| Enterprise | Contact sales | Custom | Hosted by n8n or self-hosted |
Prices checked 2026-12-05 on n8n.io/pricing, which showed euros and annual billing; the currency shown may differ by region. n8n’s docs list the self-hosted Community edition as free.
To compare that with a server of your own, try the self-hosting cost calculator and the guide to choosing a VPS for self-hosting.
Before you start
You need a 64-bit Linux server with Docker Engine and the Compose v2 plugin, which is what n8n’s production requirements list. From n8n 3.0, self-hosting requires a Docker-based deployment; npm installs are no longer supported. You also need two DNS names pointing at the server, for example n8n.example.org for the editor and hooks.example.org for webhooks.
n8n currently supports PostgreSQL 17 and 18, plus 16 for compatibility until PostgreSQL 19.1 ships, which n8n expects in February 2027. The example below uses 18.
The Compose setup
Create a folder and copy three files from the withPostgres example. First .env, with placeholders replaced, the version set to the current stable release and an N8N_ENCRYPTION_KEY line added. The repository’s sample uses the same placeholder for both user names; do not keep that. Use two different lowercase names: the init script creates the second user with an unquoted CREATE USER, which fails if the name matches the first.
N8N_VERSION=3.4.4
POSTGRES_USER=n8n_admin
POSTGRES_PASSWORD=CHANGE_ME
POSTGRES_DB=n8n
POSTGRES_NON_ROOT_USER=n8n_user
POSTGRES_NON_ROOT_PASSWORD=CHANGE_ME
RUNNERS_AUTH_TOKEN=CHANGE_ME
N8N_ENCRYPTION_KEY=CHANGE_ME
Generate the encryption key with the command n8n’s docs suggest, and paste the output into .env:
openssl rand -hex 32
Then init-data.sh, unchanged from the repository. It creates the non-root user and gives it CREATE on the public schema, which PostgreSQL 15 and later no longer grant to every user:
#!/bin/bash
set -e;
if [ -n "${POSTGRES_NON_ROOT_USER:-}" ] && [ -n "${POSTGRES_NON_ROOT_PASSWORD:-}" ]; then
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}';
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER};
GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER};
EOSQL
else
echo "SETUP INFO: No Environment variables given!"
fi
Finally docker-compose.yml. Compared with the repository, the n8n port is bound to 127.0.0.1 (as in n8n’s server guide), and the URL, proxy, timezone, file-permission and encryption-key variables from n8n’s docs are added:
volumes:
db_storage:
n8n_storage:
services:
postgres:
image: postgres:18
restart: always
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_DB
- POSTGRES_NON_ROOT_USER
- POSTGRES_NON_ROOT_PASSWORD
# postgres:18 moved its default data dir. Pinning it keeps the volume
# mount stable across major versions.
- 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:
- N8N_DEPLOYMENT_ARTIFACT=docker-compose
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
- DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
- DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
- N8N_RUNNERS_MODE=external
- N8N_RUNNERS_AUTH_TOKEN=${RUNNERS_AUTH_TOKEN}
- N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
- N8N_HOST=n8n.example.org
- N8N_PROTOCOL=https
- N8N_EDITOR_BASE_URL=https://n8n.example.org/
- N8N_WEBHOOK_URL=https://hooks.example.org/
- N8N_PROXY_HOPS=1
- GENERIC_TIMEZONE=Europe/Berlin
- TZ=Europe/Berlin
ports:
- "127.0.0.1:5678:5678"
links:
- postgres
volumes:
- n8n_storage:/home/node/.n8n
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
What the pieces do:
127.0.0.1:5678matters. Docker’s docs warn that published container ports bypass ufw rules, so a plain5678:5678would put n8n on the internet even with a firewall. n8n’s production requirements also say not to expose the n8n process directly.n8n_storageholds the.n8nfolder. n8n’s Docker page says to keep it on a volume even with PostgreSQL, because it holds the encryption key and other instance data.n8n-runnerruns Code node tasks in a separate container. Its image tag must match n8n’s.PGDATAmust stay. n8n’s example warns that without it, PostgreSQL 18 writes outside the volume and starts empty.
Start it with docker compose up -d. n8n creates its tables on first start.
The schema question: DB_POSTGRESDB_SCHEMA
n8n’s database reference lists DB_POSTGRESDB_SCHEMA as a string, default public, described simply as “The PostgreSQL schema.” Like most database variables it also accepts a _FILE variant for reading the value from a file.
If n8n has the database to itself, leave it at public. Use a custom schema when n8n shares a database with other applications and you want its tables kept apart. Two things to know:
- n8n’s docs do not say whether n8n creates the schema. Its bundled database driver does run
CREATE SCHEMA IF NOT EXISTSfor a schema other thanpublic, but that needsCREATErights on the database and is undocumented behavior. The production requirements say the database user must be able to “use and create objects in the configured schema”. Create it yourself, owned by the n8n user, before n8n’s first start. - Later maintenance has to name the schema. n8n’s update guide, for example, queries
<schema-name>.migrationsinstead ofmigrationswhen a custom schema is set.
On a fresh install, start only PostgreSQL, then create the schema with the psql pattern from n8n’s example README and the CREATE SCHEMA ... AUTHORIZATION form from PostgreSQL’s docs:
set -a && . ./.env && set +a
docker compose up -d postgres
docker compose exec -T postgres psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "CREATE SCHEMA IF NOT EXISTS n8n AUTHORIZATION n8n_user;"
Add - DB_POSTGRESDB_SCHEMA=n8n to the n8n service’s environment, then run docker compose up -d. n8n’s docs do not describe moving an existing instance from one schema to another, so set the schema on a fresh install.
Create the owner account privately
The first person to open a fresh instance sees a signup screen and becomes the owner. Do this before any proxy is live, through an SSH tunnel from your computer:
ssh -L 5678:localhost:5678 user@n8n.example.org
Then open http://localhost:5678, create the owner (n8n requires at least eight characters, one number and one capital letter), and turn on two-factor authentication under Personal settings. Each user can turn on 2FA for their own account; forcing it for all users is an Enterprise feature on self-hosted n8n. n8n’s checklist also says not to use the owner account for daily work.
Reverse proxy, TLS and a private editor
n8n’s SSL page provides this nginx configuration. It redirects HTTP to HTTPS, allows only TLS 1.2 and 1.3, sets HSTS, and passes the WebSocket and forwarded headers n8n needs. Get the certificate with a tool such as Certbot and automate renewal:
# Redirect all HTTP traffic to HTTPS
server {
listen 80;
server_name n8n.example.org;
return 301 https://n8n.example.org$request_uri;
}
server {
listen 443 ssl;
server_name n8n.example.org;
ssl_certificate /etc/letsencrypt/live/n8n.example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/n8n.example.org/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
# Tell browsers to use HTTPS only. n8n sets this header itself only when n8n terminates TLS.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
proxy_pass http://localhost:5678;
proxy_http_version 1.1;
# Needed for the editor's WebSocket connection
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Pass on information about the original request
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Do not add a Content-Security-Policy header in nginx; the docs say a proxy-level policy can break forms, binary data previews and the hosted Chat Trigger. n8n 3.0 enforces its own policy by default.
The variables in the Compose file tie into this:
N8N_WEBHOOK_URLsets the public base URL for webhooks. Without it, n8n builds URLs fromN8N_PROTOCOL,N8N_HOSTandN8N_PORTand shows the internal port 5678. It is available from 2.30.0; the olderWEBHOOK_URLstill works but is deprecated from 2.35.0.N8N_PROXY_HOPS=1tells n8n it runs behind one reverse proxy, so it uses theX-Forwarded-*headers nginx sets for the client’s address, host and protocol.N8N_EDITOR_BASE_URLis the address users open, and is used in emails.
Now the split. Some triggers need public webhook URLs, because services such as payment providers or form tools call them. The editor does not. n8n’s webhook docs describe serving webhooks and forms from their own hostname, where the editor and the internal /rest API are not reachable:
- Editor (
n8n.example.org): in thelocation /block above, add nginxallowlines for your office or VPN address range followed bydeny all;. Or skip the public server block entirely and reach the editor over a VPN or the SSH tunnel shown earlier. - Webhooks (
hooks.example.org): a second server block with its own certificate, proxying only/webhook/,/webhook-waiting/,/form/and/form-waiting/(add the-test/variants if outside services must reach test URLs). Everything else should return an error.
Also require authentication on Webhook nodes that accept outside input, and consider blocking the Execute Command, SSH and Read/Write Files from Disk nodes, as n8n’s security checklist recommends. If you plan to let AI agents call tools from n8n, the self-hosted AI agents guide covers the extra risks, and running an LLM yourself explains how to keep model calls on your own hardware.
Backups
n8n’s backup page says a complete backup has two parts: the .n8n folder, which holds the config file with the encryption key, and the PostgreSQL database. The .n8n folder is needed even with PostgreSQL, because credentials in the database are encrypted with that key. Without it, a restored database is useless for credentials.
Dump the database with the command from n8n’s example README:
set -a && . ./.env && set +a
docker compose exec -T postgres pg_dumpall -U "$POSTGRES_USER" > n8n-backup.sql
Copy the .n8n folder out of the container:
docker compose cp n8n:/home/node/.n8n ./n8n-folder-backup
Back up .env as well, since it now holds N8N_ENCRYPTION_KEY. Store the key somewhere other than the server, such as a password manager. n8n’s docs warn: “If you lose the key, n8n can’t decrypt your stored credentials and you must re-create them.” Never change the key on an existing database; n8n will not start with a different one.
The CLI export is a useful extra, not a full backup: it covers workflows and credentials but not users, executions, variables or settings. In Docker, run it as the node user:
docker exec -u node -it <n8n-container-name> n8n export:workflow --backup --output=backups/workflows/
docker exec -u node -it <n8n-container-name> n8n export:credentials --backup --output=backups/credentials/
Those folders are outside the volume, so copy them off before you re-create the container. Never run docker compose down -v: the example’s README warns it deletes the volume with your encryption key.
Updates
Pin an exact version in N8N_VERSION, as above, and change it on purpose. n8n’s update guide suggests updating at least monthly so you never jump many versions at once. Before each update:
- Read the release notes and, for major versions, the breaking changes page. n8n 3.0, stable since 3.4.1 in late November 2026, removed npm installs, turned on encryption key rotation and enforced a content security policy by default.
- Take a full backup. Changing the tag back does not reverse database migrations; the backup is your way back.
- Update
N8N_VERSION, which also moves the runner image, then run the documented steps:docker compose pull,docker compose down,docker compose up -d.
Upgrading PostgreSQL to a new major version is a separate job; the example README documents a dump-and-restore path.
For the wider picture of running AI and automation tools yourself, see the self-hosted AI hub.
Frequently asked questions
- What does DB_POSTGRESDB_SCHEMA do, and do I need it?
- It tells n8n which PostgreSQL schema to keep its tables in. The default is public, which is fine for a database that only n8n uses. Set it when n8n shares a database with other applications. n8n's code tries to create a missing schema, but its docs do not document that, so create the schema yourself before n8n's first start, owned by the n8n database user.
- What happens if I lose the n8n encryption key?
- n8n encrypts stored credentials with that key. n8n's docs say that if you lose it, n8n can't decrypt your stored credentials and you must re-create them. Back it up somewhere other than the server, and never change it on an existing database.
- Should I set WEBHOOK_URL or N8N_WEBHOOK_URL?
- On current n8n, N8N_WEBHOOK_URL. It is available from n8n 2.30.0, and WEBHOOK_URL is deprecated from 2.35.0: it still works as an alias but n8n logs a warning. On versions older than 2.30.0, only WEBHOOK_URL works.
- Is n8n open source?
- No. n8n uses its own Sustainable Use License, which n8n calls fair-code, plus a separate Enterprise License for files marked .ee. n8n's license page says it does not call itself open source, because the Open Source Initiative's definition does not allow limits on use. Internal business, personal and non-commercial use is free; offering n8n to others as a workflow-building service is not covered.
- Can the n8n editor stay private if some workflows need public webhooks?
- Yes. Set N8N_WEBHOOK_URL to a separate hostname and make only the webhook and form paths reachable there. Keep the editor hostname behind a VPN, an IP allowlist or an SSH tunnel. n8n's docs describe this split for forms behind an authentication proxy.
Sources, checked December 5, 2026
- n8n docs: Install with Docker (older page, still published: image name, PostgreSQL variables, updating)
- n8n docs: Install and run n8n using Docker Compose on a server (N8N_HOST, N8N_PROTOCOL, localhost port binding)
- n8n docs: Install using Docker Compose (PostgreSQL step, PGDATA note)
- n8n-hosting repository: docker-compose/withPostgres (compose file, .env, init-data.sh, README)
- n8n docs: Database environment variables (DB_POSTGRESDB_SCHEMA, default public)
- n8n docs: Choose n8n's database (supported PostgreSQL versions, required permissions)
- n8n docs: Production requirements (database user permissions, network, DNS)
- n8n docs: Deployment environment variables (N8N_ENCRYPTION_KEY, N8N_HOST, N8N_PROTOCOL, N8N_PROXY_HOPS, N8N_EDITOR_BASE_URL)
- n8n docs: Endpoints environment variables (N8N_WEBHOOK_URL, deprecated WEBHOOK_URL)
- n8n docs: Set a custom encryption key
- n8n docs: Configure webhook URLs with reverse proxy (separate webhook hostname)
- n8n docs: Set up SSL (NGINX example)
- n8n docs: Security checklist for production
- n8n docs: User management for self-hosted n8n (owner account setup)
- n8n docs: Two-factor authentication
- n8n docs: Manage security policies (2FA enforcement availability)
- n8n docs: Back up and restore
- n8n docs: Use the command line (running the CLI in Docker)
- n8n docs: Update self-hosted n8n
- n8n docs: v3.0 breaking changes (3.0 stable from 3.4.1)
- n8n docs: Release notes
- Docker Hub: n8nio/n8n tags (stable = 3.4.4, 4 December 2026)
- n8n repository: bundled TypeORM PostgresDriver (CREATE SCHEMA IF NOT EXISTS for a non-public schema)
- n8n repository: LICENSE.md (Sustainable Use License)
- n8n repository: LICENSE_EE.md (n8n Enterprise License)
- n8n docs: Community license
- n8n docs: License FAQ
- n8n docs: Choose how to use n8n (Cloud or self-hosted)
- n8n pricing
- PostgreSQL 18 docs: CREATE SCHEMA
- PostgreSQL 18 docs: Schemas (public schema privileges)
- Docker docs: Packet filtering and firewalls (Docker and ufw)
- Docker docs: docker compose cp
- nginx docs: ngx_http_access_module (allow and deny)
- OpenSSH ssh(1) manual (-L port forwarding)