To self-host n8n with PostgreSQL, you run three containers with Docker Compose: PostgreSQL for the data, n8n itself, and Caddy for automatic HTTPS. At the end you will have n8n on your own domain, backed by a real database, with a nightly backup.
Quick answer: Put a Docker Compose file on your VPS that runs postgres, n8n (with DB_TYPE=postgresdb and the DB_POSTGRESDB_* variables) and caddy, keep the secrets in .env, and start it with docker compose up -d.
- Point an A record such as
n8n.yourdomain.comat the server. - Create
~/n8n-stackwithdocker-compose.yml,.envandCaddyfile. - Run
docker compose up -dand check that Postgres showshealthy. - Open your domain over HTTPS and create the owner account.
- Schedule the
pg_dumpbackup and keep a copy of the encryption key.
In this guide
- Why Use PostgreSQL Instead of SQLite for n8n
- Before You Start
- Step 1: Create the Docker Compose File
- Step 2: Add the .env File and the Caddyfile
- Step 3: Start the Stack and Check It
- Step 4: Back Up the Database and the Encryption Key
- If It Still Doesn’t Work
- Tune Database Connections Under Load
- Frequently Asked Questions
- Next Steps

Why Use PostgreSQL Instead of SQLite for n8n
n8n’s default SQLite database lives in a single file inside the n8n data folder. That is fine for getting started, but n8n does not recommend it for queue mode, the setup you need to scale beyond one process.
SQLite allows only one writer at a time, so heavy parallel writes can end in SQLITE_BUSY errors, and several n8n processes cannot share the file. PostgreSQL handles concurrent writes with MVCC (Multi-Version Concurrency Control), so many executions can write at once without blocking each other. It also gives you standard tools for backups and monitoring. n8n is one of several tools covered in the self-hosted tools for developers guide.
Before You Start
- Server: A Linux VPS (a current Ubuntu LTS works well) with 2 vCPUs and at least 2GB RAM; 4GB gives comfortable headroom.
- Access: SSH access as root or a user that can run Docker. Every command below runs on the server over SSH.
- Docker: A current Docker Engine release with the Docker Compose V2 plugin.
- Domain Name: An A record (for example
n8n.yourdomain.com) pointing to the server’s public IP address, with ports 80 and 443 open so the SSL certificate can be issued. If you have not set up DNS on a VPS before, the Docker Compose on Hostinger VPS with Namecheap DNS guide walks through it. - Time: about 30 minutes, plus DNS propagation.
Check the Docker versions on the server:
docker --version
docker compose versionEach command prints a version number. If docker compose version says the command is unknown, install the Compose plugin before you continue.
Step 1: Create the Docker Compose File
The stack has three services: the PostgreSQL database, the n8n automation engine, and a Caddy reverse proxy that obtains and renews the SSL certificate. Create a directory for it on the server and move into it:
mkdir -p ~/n8n-stack && cd ~/n8n-stackCreate a file named docker-compose.yml in that directory (for example with nano docker-compose.yml) and paste the configuration below. It defines the network, the volumes and the environment variables that link the services.
networks:
n8n-network:
driver: bridge
services:
postgres:
image: postgres:16-alpine
container_name: n8n-postgres
restart: always
environment:
- POSTGRES_USER=${DB_POSTGRES_USER}
- POSTGRES_PASSWORD=${DB_POSTGRES_PASSWORD}
- POSTGRES_DB=${DB_POSTGRES_DATABASE}
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- n8n-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 5
n8n:
image: docker.n8n.io/n8nio/n8n:2.50.6
container_name: n8n-engine
restart: always
ports:
- "127.0.0.1:5678:5678"
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=${DB_POSTGRES_DATABASE}
- DB_POSTGRESDB_USER=${DB_POSTGRES_USER}
- DB_POSTGRESDB_PASSWORD=${DB_POSTGRES_PASSWORD}
- N8N_HOST=${SUBDOMAIN}.${DOMAIN}
- N8N_PORT=5678
- N8N_PROTOCOL=https
- N8N_PROXY_HOPS=1
- NODE_ENV=production
- N8N_WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN}/
- GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
networks:
- n8n-network
caddy:
image: caddy:2-alpine
container_name: n8n-proxy
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
depends_on:
- n8n
networks:
- n8n-network
volumes:
postgres_data:
n8n_data:
caddy_data:
caddy_config:The healthcheck makes n8n wait until PostgreSQL is ready to accept connections. This prevents boot-loop crashes during cold server restarts. Port 5678 is bound to 127.0.0.1, so n8n is reachable only through Caddy, not directly from the internet.
Four details are easy to get wrong:
- The database variables that n8n reads are spelled
DB_POSTGRESDB_*(withDBafterPOSTGRES). If you misspell them, n8n ignores them and tries to reach a database onlocalhost. N8N_PROXY_HOPS=1tells n8n that it sits behind one reverse proxy.N8N_WEBHOOK_URLis the current name of the webhook URL setting. n8n’s docs list the older name,WEBHOOK_URL, as deprecated from n8n 2.35.0; it still works but logs a warning on startup. If you pin an image older than 2.35.0, useWEBHOOK_URLinstead.- The n8n image is pinned to a version,
2.50.6, instead oflatest. On 2 December 2026,lateston Docker Hub moved to n8n 3.x, a new major version. n8n now treats 3.x as its stable line (from 3.4.1, released 26 November 2026), but the move from 2.x is one-way and has breaking changes, so 2.50.6 stays a safe pin until you plan that upgrade. With a pinned tag,docker compose pullnever moves you to a new major version by surprise. Before you install, check the n8n GitHub releases page for a newer 2.x release that isn’t marked Pre-release, and use that number instead.
Step 2: Add the .env File and the Caddyfile
Keep credentials out of the compose file. Create an .env file in the same directory and replace the placeholder values, using a long random password for the database:
# Database Configuration
DB_POSTGRES_USER=n8n_db_user
DB_POSTGRES_PASSWORD=super_secure_password_here
DB_POSTGRES_DATABASE=n8n_prod
# Domain Configuration
DOMAIN=yourdomain.com
SUBDOMAIN=n8n
# System Settings
GENERIC_TIMEZONE=America/New_YorkLock the file down so only your user can read it with chmod 600 .env. Docker Compose reads .env automatically when you run it from this directory.
Next, create a file named Caddyfile (no extension) next to the other two. It routes HTTPS requests to the n8n container, and Caddy gets the Let’s Encrypt certificate for the hostname on its own. Replace n8n.yourdomain.com with the same hostname you set through SUBDOMAIN and DOMAIN in .env:
n8n.yourdomain.com {
reverse_proxy n8n:5678
}Step 3: Start the Stack and Check It
From the ~/n8n-stack directory, start the services in the background:
docker compose up -dThe first run downloads the three images, which takes a minute or two. Then check that all three containers are up:
docker compose psThe postgres service should show a status of running (healthy), because it is the only one with a healthcheck; n8n and caddy should show running. Then follow the n8n logs until it reports that the editor is accessible (press Ctrl+C to stop following):
docker compose logs -f n8nOpen https://n8n.yourdomain.com in your browser. Caddy requests the certificate on first start, which can take a minute, and n8n then asks you to create the owner account. Create it straight away, because until you do, anyone who finds the URL could claim it.
Step 4: Back Up the Database and the Encryption Key
If the database volume is lost or corrupted, you lose every workflow, execution and saved credential. Back it up from day one.
The script below runs pg_dump inside the Postgres container and writes a dated .sql file to the host. Save it on the server as, for example, /opt/backups/n8n-backup.sh. If you changed the user or database name in .env, change n8n_db_user and n8n_prod to match:
#!/bin/bash
BACKUP_DIR="/opt/backups/postgres"
mkdir -p "$BACKUP_DIR"
FILENAME="$BACKUP_DIR/n8n_backup_$(date +%F_%T).sql"
docker exec n8n-postgres pg_dump -U n8n_db_user n8n_prod > "$FILENAME"
find "$BACKUP_DIR" -type f -mtime +7 -deleteThe script dumps your schema and data to /opt/backups/postgres and deletes backups older than seven days. To run it, make it executable with chmod +x /opt/backups/n8n-backup.sh, run it once by hand with root rights, and check that a non-empty .sql file appears. Then schedule it with sudo crontab -e and a line such as 0 3 * * * /opt/backups/n8n-backup.sh for a run every night at 03:00.
The database dump is not a complete backup on its own. Saved credentials are encrypted with a key that lives in the n8n_data volume (or in N8N_ENCRYPTION_KEY if you set one), so keep a copy of that key somewhere safe as well. Ideally, copy the backups off the server too.
If It Still Doesn’t Work
- The browser shows a certificate error or Caddy logs ACME errors: the A record does not point at this server yet, or ports 80 and 443 are blocked. Check DNS, open the ports in your firewall, and look at
docker compose logs caddy. - 502 Bad Gateway right after start: n8n is still starting or running database migrations. Wait a minute and watch
docker compose logs -f n8n. - n8n logs a connection error to
localhost:5432or127.0.0.1: aDB_POSTGRESDB_*variable is misspelt or missing, so n8n fell back to its defaults. Compare the names with the compose file above. - “password authentication failed” after you changed the password in
.env: the Postgres image appliesPOSTGRES_PASSWORDonly when it creates an empty database. Change the password inside Postgres as well, or start over by removing the volume if the instance holds no data yet. - Webhook URLs in the editor show the wrong host or
:5678: the webhook URL variable is not reaching the container. Check theN8N_WEBHOOK_URLline and recreate the stack withdocker compose up -d.
Tune Database Connections Under Load
n8n keeps a small pool of connections to PostgreSQL: DB_POSTGRESDB_POOL_SIZE defaults to 2. If executions slow down, or the n8n logs show database connection timeouts while many workflows run in parallel, look at the logs first:
docker compose logs n8nThen raise the pool size by adding one variable to the environment list of the n8n service in docker-compose.yml:
- DB_POSTGRESDB_POOL_SIZE=10Increase it gradually; n8n’s documentation warns that too many connections can hurt performance. PostgreSQL itself allows 100 connections by default, which is plenty for a single n8n instance. You only need to raise that if several n8n instances or workers share the database, by adding a command to the postgres service:
command: postgres -c max_connections=200Run docker compose up -d again to recreate the containers with the new settings.
Frequently Asked Questions
How do I update n8n to the latest version?
Because the image is pinned, docker compose pull on its own downloads nothing new. Take a database backup first. Then open docker-compose.yml, change the tag on the n8n image (for example from 2.50.6 to a newer 2.x release from the GitHub releases page) and run these commands on the server in ~/n8n-stack:
docker compose pull
docker compose up -d --remove-orphansYour workflows, credentials and settings live in Docker volumes, so they survive the update. n8n applies any database migrations on startup.
Moving to n8n 3.x is a major upgrade, so don’t just change the tag. First open Settings, then Migration Report in your n8n editor, read the n8n 3.0 breaking changes, and take a fresh backup. n8n says the only way back to 2.x after the upgrade is to restore a backup. If you run n8n on Railway rather than your own server, follow how to update n8n on Railway instead.
Can I run n8n on a cheap 1GB RAM VPS?
It can boot, but it is not recommended for production. Memory use jumps when a workflow handles large JSON payloads, and on 1GB the Linux out-of-memory (OOM) killer may stop the n8n container mid-run. Start with at least 2GB of RAM, or add swap if you must stay on a small plan.
Where are my workflow credentials stored?
They are stored encrypted in the PostgreSQL database, using a key n8n generates on first start and keeps in the n8n_data volume. To set your own key, add N8N_ENCRYPTION_KEY to .env and pass it to the container with - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY} in the n8n service’s environment list before the first run.
How does n8n handle database migrations?
Automatically. When you start a newer n8n image, it checks the schema version of your PostgreSQL database and applies any needed migrations before the editor starts. That is why you should back up before every upgrade: going back to an older image after a migration has run is not a simple swap.
Is n8n the right tool, or should I use Zapier or Make?
Self-hosted n8n suits you if you are comfortable running a server and want no per-task pricing. Zapier and Make are easier to start with but charge by usage. The Zapier vs Make vs n8n comparison covers the trade-offs in detail.
Next Steps
Run the backup script once, then practise a restore into a test database so you know it works. After that, check for a new n8n release every few weeks and raise the pinned tag as the FAQ above describes. When one process is no longer enough, look into n8n’s queue mode with Redis to add workers. If you want a lighter open-source alternative to compare, see how to self-host Activepieces with Docker Compose.

