n8n is an automation platform with a visual editor: you wire nodes into workflows that take data from webhooks, schedules and external services and pass it along. The cloud version is great for getting started, but how many executions you get and what features you can use depend on your subscription. Self-hosting n8n on your own VPS moves those limits to your server: you decide how many workflows to run and where your data and keys live.
In this guide you'll install n8n on a VPS with Docker Compose and PostgreSQL, expose it over HTTPS through nginx and certbot, and set up backups and updates.
What you need
- a Linux VPS with SSH access and a sudo user;
- Docker and the Docker Compose plugin, see Docker on a VPS;
- a domain such as n8n.example.com with an A record pointing to the server's IP, see how to point a domain to your VPS;
- nginx and certbot for HTTPS, as covered in how to host a website on a VPS with Nginx and SSL.
For comfortable headroom, go with the Start plan: 2 vCPU, 4 GB RAM and 35 GB of disk. n8n, PostgreSQL and a few data-processing workflows run smoothly on that. On a 1 GB plan, the database and n8n end up fighting over memory, and workflows that handle large files may crash.
Project layout
Create a project directory with two files: docker-compose.yml describing the services and .env holding the secrets. Keep .env out of git and out of any public directory.
mkdir -p ~/n8n && cd ~/n8n
nano docker-compose.yml
nano .env
chmod 600 .envdocker-compose.yml with PostgreSQL
The setup has two services: postgres stores workflows, credentials and execution history, and n8n talks to it over the internal Compose network. The n8n port is bound to localhost, so the service isn't reachable from outside directly. Traffic only gets in through nginx.
services:
postgres:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: n8n
volumes:
- n8n_pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
interval: 10s
timeout: 5s
retries: 5
n8n:
image: n8nio/n8n:latest
restart: unless-stopped
depends_on:
postgres:
condition: service_healthy
environment:
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_PORT: 5432
DB_POSTGRESDB_DATABASE: n8n
DB_POSTGRESDB_USER: n8n
DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
N8N_HOST: n8n.example.com
N8N_PORT: 5678
N8N_PROTOCOL: https
WEBHOOK_URL: https://n8n.example.com/
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
GENERIC_TIMEZONE: Europe/Moscow
TZ: Europe/Moscow
ports:
- "127.0.0.1:5678:5678"
volumes:
- n8n_data:/home/node/.n8n
volumes:
n8n_pgdata:
n8n_data:A few variables worth calling out. N8N_HOST is just the hostname, no protocol. WEBHOOK_URL is the public address external services will call your webhooks on, and it has to match the domain in nginx. N8N_PROTOCOL must be https when n8n sits behind an HTTPS proxy. GENERIC_TIMEZONE and TZ drive the schedules in cron nodes, so set them to your own time zone, for example America/New_York or UTC.
The latest tag is fine for a first run. For a production setup, pin a specific n8n version you've tested, like n8nio/n8n:<version>.
The .env file with secrets
POSTGRES_PASSWORD=replace-with-long-random-password
N8N_ENCRYPTION_KEY=replace-with-long-random-stringGenerate long random values with openssl rand -hex 32. The database password and the encryption key should be different.
Start the containers
cd ~/n8n
docker compose up -d
docker compose ps
docker compose logs -f n8nOnce the logs say the editor is up, n8n is listening on port 5678 on localhost. Until nginx is configured, you can't open n8n in a browser, and that's exactly how it should be.
Expose n8n through nginx with HTTPS
nginx accepts requests on ports 80 and 443, gets a certificate via certbot and proxies traffic to n8n. The editor and some features rely on WebSockets, so the Upgrade and Connection headers are a must. Create a site config:
server {
listen 80;
server_name n8n.example.com;
client_max_body_size 16m;
location / {
proxy_pass http://127.0.0.1:5678;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300s;
}
}sudo nano /etc/nginx/sites-available/n8n
sudo ln -s /etc/nginx/sites-available/n8n /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d n8n.example.comcertbot adds a server block for port 443 and, if you want, an HTTP-to-HTTPS redirect. Certificate issuance and auto-renewal are covered in how to host a website on a VPS with Nginx and SSL. Don't open port 5678 to the world: in your firewall, allow only 22, 80 and 443.
First login and the owner account
Open https://n8n.example.com. On first visit, n8n asks you to create an owner account: enter an email and a long, unique password. This account has full access to the instance, so don't share it. If your n8n version supports two-factor authentication, turn it on in your profile settings.
Older n8n versions used basic auth via the N8N_BASIC_AUTH_* variables. Current versions protect access with the owner account instead. Check the docs for your version before relying on a specific mechanism. The one rule that always applies: the editor must never be open without a login, so create the owner account right after the first start.
Backing up the database and data
Two things matter for n8n. First, the PostgreSQL database: workflows, encrypted credentials and execution history. Second, the /home/node/.n8n directory in the n8n_data volume, where n8n keeps its internal files. The encryption key lives separately, and without it the database won't give you back your saved credentials.
cd ~/n8n
mkdir -p backups
docker compose exec -T postgres pg_dump -U n8n -d n8n | gzip > backups/n8n-db-$(date +%F).sql.gz
docker run --rm -v n8n_n8n_data:/data:ro -v "$(pwd)/backups":/backup alpine tar czf /backup/n8n-data-$(date +%F).tar.gz -C /data .The volume name depends on the project directory name: here the directory is called n8n, so the volume becomes n8n_n8n_data. Check the exact name with docker volume ls.
Store backups off the server: in S3-compatible storage, on another VPS or on your own machine. A backup on the same disk won't help if the server itself dies. Test a restore on a staging box once a month. A backup you've never restored is just a hope.
To restore the database, reverse the process: bring up PostgreSQL with compose, then pipe the dump through gunzip -c backups/n8n-db-DATE.sql.gz into docker compose exec -T postgres psql -U n8n -d n8n. Stop n8n first with docker compose stop n8n.
Updating n8n
Take a fresh backup before every update. Then pull the new image and recreate the containers:
cd ~/n8n
docker compose pull
docker compose up -d
docker compose logs --tail=50 n8nMajor releases can change how certain nodes behave and run database migrations. Read the release notes before you update, test your key workflows afterward, and only then call the update done. If something breaks, roll back to the previous tag in docker-compose.yml and restore the database from backup if the migration already ran.
Security for self-hosted n8n
- Don't publish ports 5678 and 5432 to the internet. In the config above, PostgreSQL has no ports section at all.
- Keep .env at mode 600 and never commit it to git.
- Give the API keys you connect to n8n only the permissions each workflow needs.
- Don't put plaintext passwords in workflows: use n8n credentials.
- Keep the OS and Docker updated, and follow the basics in Linux server security basics.
Common installation issues
- Webhook URLs show http instead of https: check WEBHOOK_URL and N8N_PROTOCOL, then recreate the container with docker compose up -d.
- The editor loads but doesn't update: nginx is most likely missing the Upgrade and Connection headers.
- n8n won't start and reports a database connection error: check the password in .env and the status of the postgres container.
- certbot fails to issue a certificate: the domain must point to the server's IP, and ports 80 and 443 must be reachable from outside.
If a workflow runs fine in the editor but never fires from a webhook, check the public address in WEBHOOK_URL first and make sure the domain is reachable from the internet.