How to Self-Host n8n with PostgreSQL in 2026
A step-by-step tutorial for self-hosting n8n with PostgreSQL on a single Linux server using Docker Compose. Covers .env configuration, encryption keys, TLS via Caddy, persistence and backup strategy, queue mode for higher throughput, and the most common operational errors encountered during deployment.
Overview
n8n is an open-source workflow automation tool that competes with Zapier and Make. Unlike most competitors, n8n can be self-hosted on any Docker-capable server. By default n8n uses an embedded SQLite database; for any deployment beyond a single-user lab, swapping SQLite for PostgreSQL is the recommended next step. PostgreSQL handles concurrent writes, supports point-in-time recovery, and is required for the multi-process queue mode used in higher-throughput deployments.
This tutorial covers a production-ready single-server n8n + PostgreSQL stack using Docker Compose, including persistence, environment configuration, queue mode, and operational notes.
Prerequisites
- A Linux server with at least 2 GB RAM and 2 vCPU (4 GB recommended for queue mode)
- Docker Engine 24.0+ and Docker Compose v2
- A registered domain with DNS pointing at the server (n8n requires HTTPS for many OAuth integrations)
- Inbound firewall rules permitting ports 80 and 443
Architecture
A production n8n deployment with PostgreSQL has these components:
- n8n: Main n8n process (UI + API, accepts webhook traffic)
- postgres: Workflow data, credentials, execution history
- redis: Queue backend (only required for queue mode)
- n8n-worker: One or more worker containers that execute workflows (queue mode only)
- caddy or another reverse proxy for TLS termination
Step 1: Create the Project Directory
mkdir -p /opt/n8n/{data,postgres-data,redis-data}
cd /opt/n8n
Step 2: Create the .env File
Generate strong secrets and a 32-character encryption key. Credentials in n8n are encrypted with this key; losing it makes all stored credentials unrecoverable.
cat > .env <<EOF
N8N_HOST=n8n.example.com
N8N_PROTOCOL=https
N8N_PORT=5678
WEBHOOK_URL=https://n8n.example.com/
GENERIC_TIMEZONE=Europe/London
DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=postgres
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_USER=n8n
DB_POSTGRESDB_PASSWORD=$(openssl rand -hex 24)
N8N_ENCRYPTION_KEY=$(openssl rand -hex 16)
N8N_USER_MANAGEMENT_JWT_SECRET=$(openssl rand -hex 32)
EOF
chmod 600 .env
Back up the .env file off-server. Treat the encryption key like a master credential.
Step 3: Create the docker-compose.yml
version: "3.8"
services:
postgres:
image: postgres:15
restart: unless-stopped
environment:
POSTGRES_DB: ${DB_POSTGRESDB_DATABASE}
POSTGRES_USER: ${DB_POSTGRESDB_USER}
POSTGRES_PASSWORD: ${DB_POSTGRESDB_PASSWORD}
volumes:
- ./postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_POSTGRESDB_USER}"]
interval: 5s
timeout: 5s
retries: 10
n8n:
image: n8nio/n8n:1.x
restart: unless-stopped
ports:
- "5678:5678"
environment:
- N8N_HOST
- N8N_PROTOCOL
- N8N_PORT
- WEBHOOK_URL
- GENERIC_TIMEZONE
- DB_TYPE
- DB_POSTGRESDB_HOST
- DB_POSTGRESDB_PORT
- DB_POSTGRESDB_DATABASE
- DB_POSTGRESDB_USER
- DB_POSTGRESDB_PASSWORD
- N8N_ENCRYPTION_KEY
- N8N_USER_MANAGEMENT_JWT_SECRET
volumes:
- ./data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
volumes:
postgres-data:
n8n-data:
Pin the n8nio/n8n tag to a specific minor version (for example 1.85) rather than latest. n8n minor releases occasionally include database migrations that require downtime to apply.
Step 4: First Boot
docker compose up -d
docker compose logs -f n8n
On first boot n8n runs database migrations. Wait until the log shows Editor is now accessible via: before connecting. Open http://<server-ip>:5678, complete the owner setup form, and create the first user.
Step 5: Add TLS via Caddy
Add a Caddy reverse proxy for automatic Let's Encrypt certificates. Add this service to the compose file:
caddy:
image: caddy:2
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy-data:/data
depends_on:
- n8n
volumes:
caddy-data:
And a Caddyfile:
n8n.example.com {
reverse_proxy n8n:5678
encode gzip
}
Restart the stack: docker compose up -d. Caddy issues a certificate within 30-60 seconds. Once HTTPS is live, remove the 5678:5678 port mapping from the n8n service so the only entry point is Caddy.
Step 6: Persistence and Backups
- PostgreSQL data lives in
./postgres-data. Usepg_dumpdaily via cron:
0 3 * * * docker compose exec -T postgres pg_dump -U n8n n8n | gzip > /backups/n8n-$(date +\%F).sql.gz
- n8n data directory at
./datacontains binary credentials and node modules; back it up alongside the database - Encryption key must be backed up separately; without it, restored credentials cannot be decrypted
Step 7: Queue Mode (for Higher Throughput)
By default n8n runs all workflow executions in the same process as the UI/API. For workloads above approximately 50 concurrent executions or for long-running workflows that should not block webhook responses, switch to queue mode.
Add Redis and a worker service to the compose file:
redis:
image: redis:7-alpine
restart: unless-stopped
volumes:
- ./redis-data:/data
n8n-worker:
image: n8nio/n8n:1.x
restart: unless-stopped
command: worker
environment:
- DB_TYPE
- DB_POSTGRESDB_HOST
- DB_POSTGRESDB_PORT
- DB_POSTGRESDB_DATABASE
- DB_POSTGRESDB_USER
- DB_POSTGRESDB_PASSWORD
- N8N_ENCRYPTION_KEY
- EXECUTIONS_MODE=queue
- QUEUE_BULL_REDIS_HOST=redis
depends_on:
- redis
- postgres
Add EXECUTIONS_MODE=queue and QUEUE_BULL_REDIS_HOST=redis to the main n8n service environment as well. Scale workers with docker compose up -d --scale n8n-worker=3.
Common Errors
ECONNREFUSED postgres:5432: Postgres still starting; thedepends_onhealthcheck handles this on first bootCould not decrypt credentials: The encryption key changed since credentials were saved. Restore the original key or re-enter credentials- OAuth callback errors:
WEBHOOK_URLmust match the public HTTPS URL exactly; trailing slash matters out of memoryduring workflow execution: Default node memory is 256 MB. Increase viaNODE_OPTIONS=--max-old-space-size=2048in the n8n service environment- Timezone-shifted scheduled triggers: Set
GENERIC_TIMEZONEandTZin the environment; both Postgres and n8n need consistent timezones
Operating Cost
A self-hosted n8n stack on a single Hetzner CCX13 (4 vCPU, 16 GB RAM) handles approximately 10,000 workflow executions per day comfortably. At approximately €15/month this compares favourably to the n8n Cloud Starter plan at $24/month for 5,000 executions, although the comparison ignores operational overhead.
Editor's Note: ShadowGen runs this exact stack for an internal automation orchestrator handling roughly 12,000 executions per day across 70 active workflows. Hardware: a single Hetzner CCX23 (8 vCPU, 32 GB RAM, approximately €30/month). We moved to queue mode at execution number 4,000/day after webhooks started timing out behind long-running workflows; the change took roughly 90 minutes including Redis bring-up and worker scaling. The single biggest mistake we made was running 6 weeks without
pg_dumpcron — a botched n8n upgrade corrupted the schema and the only saving grace was that Hetzner snapshots existed at the VM level. Lesson: schedule thepg_dumpcron on day one, not day forty.
Tools Mentioned
Related Guides
Moxo vs Gumloop in 2026: Texting the Client, or Building the Agent
Gumloop (founded 2023) builds AI agents a company's own staff run; Moxo, whose predecessor Moxtra was co-founded in 2012, runs processes that wait on people outside the company. Gumloop wins three rounds to two: the way in, per-tool gating and a priced overage. Moxo wins on…
Moxo vs Relay.app in 2026: Where Relay's Approval Workflows Can Go Next
Relay.app, founded in 2021, shut down in September 2026 and deleted every account, workflow and run history; Moxo, whose predecessor Moxtra was co-founded in 2012, is one destination for its human-in-the-loop users. Relay.app takes two rounds, Moxo one, and vendor risk is a draw…
Moxo vs Workato in 2026: The Integration Estate, or the Approval Inside It
Workato, founded in 2013, is an enterprise integration platform whose Workflow apps add human tasks to recipes; Moxo, whose predecessor Moxtra was co-founded in 2012, ships the human step itself. Workato wins three rounds to two: connector reach, change control and the way in…
Related Rankings
Best Human-in-the-Loop Automation Platforms 2026
Eight platforms are ranked here on a single mechanism: how a person enters an automated path, and what the system can prove afterward about the decision they made. Moxo scores 7.7 and ranks first; Camunda 8 scores 7.2, Microsoft Power Automate 7.1, Pega 6.9, n8n 6.7, Zapier 5.9…
Best Automation Platforms for AI Orchestration 2026
This ranking answers one question: how many real business applications can an AI agent act on out of the box? It evaluates nine platforms as of August 2026 on the reach they give an agent, not on the workflow logic they can express. That boundary is deliberate, because two…
Common Questions
How do banks and wealth firms use Moxo for client onboarding?
Moxo's financial-services page (read 13 October 2026) pitches account opening and KYC reviews as shared work: the client gets a secure link to see what is needed and sign, while relationship managers, compliance and operations staff review and approve inside one tracked flow. The…
Can healthcare teams run patient-facing workflows on Moxo?
Yes for coordination work, once a signed BAA is in hand: Moxo told Automation Atlas in writing on 6 October 2026 that it signs BAAs, but it publishes none and did not say which plans the agreement covers or whether it reaches Moxo AI. Moxo says it has no native EHR, HL7 or FHIR…
Who is accountable when an AI agent acts in a Moxo workflow?
The company that deploys the agent is accountable: Moxo's Terms and Conditions (read 13 October 2026) make the customer "solely responsible for any AI Content, including the accuracy, quality, appropriateness, and legality thereof". In the workflow, a named person owns each human…
How does Moxo keep humans in control when AI agents run a workflow?
Moxo keeps people on the decisions by design: approvals and other human steps are ones its product page says "only a person can close", and AI agents can fill preparer, advisor or reviewer slots around them (both read 15 September 2026). The checks on AI output are opt-in…