n8n Docker Compose with PostgreSQL Production Setup
In this article
This article explains how to set up n8n in production using Docker Compose with PostgreSQL as the database. You will learn the recommended compose file, environment variables, backup strategy, and common pitfalls to avoid.
Why PostgreSQL for n8n in Production
n8n defaults to SQLite for simple installations, but SQLite is not designed for concurrent writes and can lock up under load. PostgreSQL handles multiple workflows and users reliably, supports point-in-time recovery, and is the database n8n officially recommends for production. Running n8n and PostgreSQL in Docker Compose keeps the deployment reproducible and easy to update.
Prerequisites
- Docker Engine 20.10+ and Docker Compose v2 (check with
docker compose version) - A Linux server (Ubuntu 22.04/24.04 are common) with at least 2 GB RAM and 20 GB free disk
- Domain name pointing to your server if you want HTTPS
- Basic familiarity with the command line and environment variables
Production Docker Compose File
Create a directory for your deployment, e.g. n8n-prod, and inside it create docker-compose.yml with the following content:
services:
n8n:
image: n8nio/n8n:latest
restart: unless-stopped
ports:
- "5678:5678"
environment:
- N8N_DATABASE_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=change-me-strong-password
- N8N_HOST=n8n.example.com
- N8N_PROTOCOL=https
- N8N_PORT=5678
- N8N_ENCRYPTION_KEY=your-32+char-random-key
- N8N_USER_MANAGEMENT_JWT_SECRET=another-long-random-string
- N8N_RUNNERS_ENABLED=true
- GENERIC_TIMEZONE=UTC
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres:16-alpine
restart: unless-stopped
environment:
- POSTGRES_USER=n8n
- POSTGRES_PASSWORD=change-me-strong-password
- POSTGRES_DB=n8n
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
interval: 10s
timeout: 5s
retries: 5
volumes:
n8n_data:
postgres_data:
Replace the passwords and secrets with strong random values. Generate them with openssl rand -hex 32 or openssl rand -base64 32. The N8N_ENCRYPTION_KEY is critical: it encrypts credentials stored in the database. If you lose it, you cannot recover existing credentials.
Environment Variables Explained
Key variables control how n8n connects to PostgreSQL and behaves externally:
N8N_DATABASE_TYPE=postgresdb– tells n8n to use PostgreSQL instead of SQLite.DB_POSTGRESDB_HOST– the hostname of the PostgreSQL container (herepostgres).DB_POSTGRESDB_USER/DB_POSTGRESDB_PASSWORD– credentials n8n uses to connect. Must match the POSTGRES_USER/PASSWORD in the postgres service.N8N_HOST– your public domain, used for webhooks and links in emails.N8N_PROTOCOL– set tohttpswhen behind a reverse proxy with TLS.N8N_ENCRYPTION_KEY– used to encrypt credentials. Back it up securely.N8N_USER_MANAGEMENT_JWT_SECRET– signs JWT tokens for user sessions.N8N_RUNNERS_ENABLED=true– enables the new code runner sandbox (recommended for security).
Reverse Proxy and HTTPS
Exposing n8n directly on port 5678 without TLS is insecure, especially if you log in from the internet. Use a reverse proxy like Caddy, Nginx, or Traefik to terminate TLS. A minimal Caddyfile:
n8n.example.com {
reverse_proxy n8n:5678
}
With Caddy, HTTPS certificates are automatic. For Nginx, you would manage certificates with Certbot. Ensure your firewall only allows ports 80/443 (and 22 for SSH), not 5678, from the outside.
Backup and Restore
Your data lives in the PostgreSQL volume and the n8n files volume (workflow images, settings). Back up both. For PostgreSQL, use pg_dump:
docker compose exec postgres pg_dump -U n8n n8n > backup.sql
To restore:
cat backup.sql | docker compose exec -T postgres psql -U n8n n8n
Also copy the n8n_data volume contents. Automate this with a cron job that runs the dump and copies it to a remote location. Test your backups regularly.
Common Problems and Fixes
- n8n won't start – Check logs with
docker compose logs n8n. Often it's a wrong DB password or the encryption key not being set. - Database connection refused – Ensure the postgres container is healthy before n8n starts (the
depends_oncondition helps). Also check that DB_POSTGRESDB_HOST matches the service name. - Webhook URLs use localhost – Set
N8N_HOSTandN8N_PROTOCOLcorrectly. Restart n8n after changing. - Slow performance – If you have many workflows, increase PostgreSQL memory settings or use a managed database. Also consider setting
N8N_CONCURRENCY_PRODUCTION_LIMITto a reasonable value (default is 10). - Lost encryption key – You cannot recover encrypted credentials. Always back up the key.
Upgrading n8n
To update, pull the new image and recreate containers:
docker compose pull n8n
docker compose up -d n8n
Before upgrading, back up your database and n8n files. n8n runs database migrations automatically on start, but it's safer to snapshot the database first (e.g., with pg_dump).
Frequently Asked Questions
Can I use an external PostgreSQL instead of a container?
Yes. Remove the postgres service from the compose file and set DB_POSTGRESDB_HOST to your external host. Make sure the database is reachable and the user has the right privileges. This is common when using a managed cloud database.
How do I set up n8n with a custom domain and SSL?
Use a reverse proxy as described above. Caddy is the simplest for automatic HTTPS. Set N8N_HOST to your domain and N8N_PROTOCOL to https. Then point your domain to the server and access n8n via https://yourdomain.com.
What is the default n8n port and can I change it?
The default port is 5678. To change it, edit the ports mapping in the compose file, e.g., "8080:5678". Also set N8N_PORT to the internal port (5678) if you change the container port, but the external port is what you access.
Is SQLite ever okay for production?
For very small single-user instances with low traffic, SQLite can work, but it is not recommended. PostgreSQL is more robust, supports concurrent writes, and is easier to scale. The extra setup is minimal, so use PostgreSQL from the start.
How do I back up the encryption key?
Store the N8N_ENCRYPTION_KEY in a secure place like a password manager or a file with restricted permissions. If you lose it, you lose access to all credentials encrypted with it. Some users store it in a .env file that is backed up separately.
Can I run multiple n8n instances with one PostgreSQL?
Yes, but they must share the same encryption key and JWT secret. However, running multiple instances against the same database can cause conflicts with workflow execution. It's better to run one instance and scale with workers if needed.