On this page
Key takeaways
- Only Caddy publishes ports, so n8n and the database are reachable inside Docker only.
N8N_ENCRYPTION_KEYencrypts the credentials that workflows store: if you lose it, those credentials cannot be read again, even from a backup.- You should see SSH, 80 and 443, and nothing for 5678 or 5432. Docker publishes ports around ufw, so this list is the reliable check.
n8n connects services and automates tasks with workflows. Run on your own server it keeps your workflows and the credentials they use under your control. This guide sets up n8n with PostgreSQL behind a reverse proxy that gets the HTTPS certificate for you.
Before you start
You need an Ubuntu 24.04 server with Docker and the Compose plugin, a user with sudo and a domain name you control. If you are missing one of these, the first-hour and Docker guides come first; links are at the end of this page. Commands use sudo docker; leave sudo out if your user is in the docker group.
- an A record (and an AAAA record for IPv6 if you want it) for a name such as n8n.example.com that points to the server
- ports 80 and 443 reachable from the internet: the proxy needs 80 to get its certificate
- a password manager or another place outside the server to keep one secret key (step 2 explains which)
The steps
Point the domain at the server
Create the DNS record at your domain provider, wait a few minutes and check that the name resolves to your server’s address. Without this the certificate step in step 5 fails. Use your real name instead of n8n.example.com everywhere.
getent hosts n8n.example.comCreate the folder and the secrets
The file .env holds the settings and two random secrets generated on the server. Edit the domain in it afterwards, for example with nano .env. N8N_ENCRYPTION_KEY encrypts the credentials that workflows store: if you lose it, those credentials cannot be read again, even from a backup.
mkdir -p ~/n8n && cd ~/n8n
cat > .env <<EOF
N8N_HOST=n8n.example.com
POSTGRES_PASSWORD=$(openssl rand -hex 24)
N8N_ENCRYPTION_KEY=$(openssl rand -hex 32)
TIMEZONE=Etc/UTC
EOF
chmod 600 .envCopy that one line to your password manager now.
Write compose.yaml
Save this file in the same folder. It starts PostgreSQL, n8n and Caddy. Only Caddy publishes ports, so n8n and the database are reachable inside Docker only. Caddy obtains and renews the HTTPS certificate for the domain in N8N_HOST by itself. The n8n image has no version tag here, which gives you the latest release at each pull; for steady operation pin a version you have read the release notes of.
services:
postgres:
image: postgres:16
restart: unless-stopped
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: n8n
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
interval: 5s
timeout: 5s
retries: 10
n8n:
image: docker.n8n.io/n8nio/n8n
restart: unless-stopped
environment:
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_DATABASE: n8n
DB_POSTGRESDB_USER: n8n
DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
N8N_HOST: ${N8N_HOST}
N8N_PROTOCOL: https
WEBHOOK_URL: https://${N8N_HOST}/
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
GENERIC_TIMEZONE: ${TIMEZONE}
TZ: ${TIMEZONE}
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
caddy:
image: caddy:2
restart: unless-stopped
command: caddy reverse-proxy --from ${N8N_HOST} --to n8n:5678
ports:
- "80:80"
- "443:443"
volumes:
- caddy_data:/data
depends_on:
- n8n
volumes:
postgres_data:
n8n_data:
caddy_data:Open the web ports in the firewall
Only 80 and 443 are needed. SSH should already be allowed from the first-hour setup.
sudo ufw allow 80,443/tcp
sudo ufw statusStart it
The first start downloads the images and takes a while. When docker compose ps shows all three services as running, the n8n log should end with a line saying the editor is accessible.
cd ~/n8n
sudo docker compose up -d
sudo docker compose ps
sudo docker compose logs --tail 50 n8nOpen it and create the owner account
Open https://n8n.example.com in a browser. n8n asks you to create the owner account on first visit: do it at once, because until then anyone who finds the address could. From your computer you can also check the answer of the proxy.
curl -I https://n8n.example.comCheck what listens
You should see SSH, 80 and 443, and nothing for 5678 or 5432. Docker publishes ports around ufw, so this list is the reliable check.
sudo ss -ltnpBack up the database
This writes a dump of the n8n database to a file. Copy it off the server together with the encryption key from step 2; one without the other does not bring your credentials back.
cd ~/n8n
sudo docker compose exec -T postgres pg_dump -U n8n n8n > n8n-backup.sql
ls -lh n8n-backup.sqlA copy on the same disk does not protect you from losing the server.
Update
Make a backup first and read the n8n release notes for breaking changes. Then pull the new images and recreate the containers; your data stays in the volumes.
cd ~/n8n
sudo docker compose pull
sudo docker compose up -d
sudo docker compose psLet an assistant do it, with a plan first
If you use an AI assistant that can run SSH commands, paste the prompt below after you replace the domain. It works with SSH access as a user with sudo, on a server that has Docker. If the assistant should also order the server first, give it a Cloudvix API key rather than your account login: keys have limits (up to €500 a month and 25 servers), and you can tell the assistant to show the price and wait for your yes before it orders.
The prompt makes the assistant show a plan before it acts, keep the secrets out of the chat and verify the result with real checks. The plan is the moment to look at what will be created.
You are helping me install n8n with Docker Compose on my Ubuntu 24.04 server at SERVER_IP. Connect over SSH as ops, which has sudo. Docker and the Compose plugin are installed. The domain n8n.example.com already points at the server (replace it with my real domain before you start).
Rules:
1. Before you change anything, show me a numbered plan and wait for my yes.
2. After every step, run a check command and show me its output.
3. If a check fails, stop and tell me. Do not try to work around it.
Plan to propose: create ~/n8n; write .env with N8N_HOST, TIMEZONE=Etc/UTC and two random secrets generated on the server with openssl rand, mode 600, and never print the secrets or send them to me; write compose.yaml with three services: PostgreSQL 16 with a health check, n8n (image docker.n8n.io/n8nio/n8n, PostgreSQL as its database, WEBHOOK_URL with https and the domain, a data volume) and Caddy as reverse proxy with automatic HTTPS; only Caddy may publish ports, 80 and 443; run docker compose up -d; check that docker compose ps shows all three services running, that the n8n log has no errors and that curl -I https://n8n.example.com answers with a success or redirect status; allow 80 and 443 in ufw.
Do not publish the n8n or PostgreSQL port, and do not change anything else on the server. At the end, tell me where .env is, remind me to keep a copy of N8N_ENCRYPTION_KEY somewhere outside this server, and list how to update, back up and undo everything.Settings used in this guide
| Variable | Example | What it does |
|---|---|---|
| N8N_HOST | n8n.example.com | The public name n8n is reached at. |
| N8N_PROTOCOL | https | Tells n8n that visitors arrive over HTTPS through the proxy. |
| WEBHOOK_URL | https://n8n.example.com/ | The address n8n shows for webhook nodes. A wrong value gives wrong webhook links. |
| GENERIC_TIMEZONE | Etc/UTC | The time zone for schedule triggers. |
| N8N_ENCRYPTION_KEY | 64 hex characters | Encrypts saved credentials. Keep it; never change it on a running setup. |
| POSTGRES_PASSWORD | 48 hex characters | The database password. The commands above generate it into .env. |
If something goes wrong
The browser shows a certificate error
The DNS record is missing or has not spread yet, or port 80 is blocked. Read the Caddy log with sudo docker compose logs caddy; it names the reason.
n8n restarts again and again
Read sudo docker compose logs n8n. A wrong database password usually shows up as a connection error; if you changed POSTGRES_PASSWORD after the first start, the database keeps the old one.
Webhook addresses show the wrong host
WEBHOOK_URL must be the public https address with a slash at the end, built from N8N_HOST.
The server feels slow or containers are killed
Look at memory with sudo docker stats --no-stream and choose a larger plan if it is full.
Questions people ask
Is self-hosted n8n free?
The Community Edition can be self-hosted without a licence fee under n8n’s own licence. You pay for the server it runs on. Read the licence terms on the n8n site before you use it for a commercial service.
Do I need a domain name?
For anything beyond a quick test, yes. Webhooks from other services and sign-in with OAuth both need an HTTPS address, and a certificate is issued for a name, not for a bare IP address.
Can I move n8n to a bigger server later?
Yes. Copy your backup and the files .env and compose.yaml to the new server, restore the volumes, point the DNS record at the new address and start the containers with docker compose up -d.
Where to go next
Set up a regular copy of the database dump to a place off the server, and put a reminder in your calendar to update. The Docker guide covers the engine settings this setup relies on. Links are at the end of this page.
Runs Cloudvix and is responsible for the guides published here.