When you self-host n8n VPS Docker style on your own server, you run the full workflow automation platform with no per-execution fees and no vendor lock-in. n8n is a fair-code workflow automation tool: you design workflows as connected nodes in a visual editor, and the server runs them on a schedule, on webhooks, or on demand. This guide walks you through the complete setup on a fresh Ubuntu VPS: Docker and the Compose plugin, a compose file pairing n8n with Postgres, an encryption key for your credentials, HTTPS through a reverse proxy, a first test workflow, updates, and backups.
A self hosted n8n install is real infrastructure, not a side experiment. Follow each step in order and you end up with a stable server you can trust with daily automations.
What You Need to Self-Host n8n on a VPS with Docker
Before touching the terminal, get these three things ready:
- A VPS running Ubuntu 22.04 or 24.04. n8n is a Node.js application and is light on resources. A small VPS with 2 vCPU and 2 GB of RAM runs it comfortably for personal and small-team use.
- A domain or subdomain pointed at the VPS. Create an A record such as
n8n.example.comtoward your server IP. Webhooks and OAuth callbacks need a public HTTPS address, so a domain is not optional for real workflows. - SSH access and basic terminal comfort. Everything here runs through copy-paste shell commands.
Install Docker and the Compose Plugin
Docker packages n8n and its database as isolated containers, which keeps the install clean and upgrades boring. Install it from the official script:
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER
Log out and back in so the group change takes effect, then confirm both tools respond:
docker --version
docker compose version
Write the n8n Docker Compose File
The n8n docker compose setup runs two services: Postgres for durable storage and the n8n app itself. Postgres keeps workflows, executions, and credentials in a real database instead of a fragile SQLite file, which matters the moment a workflow becomes load-bearing. This n8n postgres docker pairing is the layout the n8n team documents for production-style installs.
Create a project folder and a compose.yaml inside it:
mkdir -p /opt/n8n && cd /opt/n8n
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"]
interval: 10s
timeout: 5s
retries: 5
n8n:
image: n8nio/n8n
restart: unless-stopped
depends_on:
postgres:
condition: service_healthy
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}/
GENERIC_TIMEZONE: Asia/Jakarta
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
ports:
- "127.0.0.1:5678:5678"
volumes:
- n8n_data:/home/node/.n8n
volumes:
postgres_data:
n8n_data:
Two details do the quiet heavy lifting here. The healthcheck plus depends_on stops n8n from starting before Postgres is ready, which is the most common cause of first-boot crashes. Binding port 5678 to 127.0.0.1 keeps n8n off the public internet; only your reverse proxy will reach it.
Set the Encryption Key and Database Password
n8n encrypts every API key and credential you save in a workflow. The key comes from N8N_ENCRYPTION_KEY. Generate long random values with OpenSSL and store them in a .env file next to the compose file:
POSTGRES_PASSWORD=$(openssl rand -hex 32)
N8N_ENCRYPTION_KEY=$(openssl rand -hex 32)
N8N_HOST=n8n.example.com
Write those three lines into /opt/n8n/.env, replacing n8n.example.com with your own domain. Then save the encryption key in your password manager too. If you lose N8N_ENCRYPTION_KEY, every stored credential becomes unreadable and you will re-enter them all by hand. There is no recovery shortcut.
Put n8n Behind a Reverse Proxy with HTTPS
n8n expects to live behind HTTPS. A reverse proxy terminates TLS and forwards traffic to the container on 127.0.0.1:5678. Caddy is the shortest path because it fetches and renews certificates on its own:
n8n.example.com {
reverse_proxy 127.0.0.1:5678
}
Install Caddy, drop that block into its config with your domain, and reload. Point your browser at https://n8n.example.com. You should see the n8n setup screen with a valid certificate. If you prefer Nginx, the same idea applies: proxy pass to 127.0.0.1:5678 and add a certificate with Certbot.
Start n8n and Create the Owner Account
Launch the stack and watch the logs for the first run:
cd /opt/n8n
docker compose up -d
docker compose logs -f n8n
Once the log shows the editor is listening, open your domain in a browser. n8n asks you to create an owner account on first visit. Use a strong password and an email you actually read, because password resets go there.
Build Your First Test Workflow
Confirm the whole pipeline works before building anything serious. Create a workflow with three nodes:
- Schedule Trigger set to every 5 minutes.
- HTTP Request pointed at a harmless public endpoint such as
https://api.github.com/zen. - Set node that keeps only the response text.
Hit Execute Workflow and check the Executions list on the left. A green entry with the fetched text means the database, the app, and HTTPS all work. Flip the Active toggle in the top right when you want it to run on its schedule without you watching.
Update n8n Without Losing Data
Upgrades are two commands because your data lives in named volumes, not inside the container image:
cd /opt/n8n
docker compose pull
docker compose up -d
docker image prune -f
Read the release notes before jumping a major version; n8n occasionally changes behavior between majors. If you want controlled upgrades, pin the image tag (for example n8nio/n8n:1.110.0, using the current version you see on the official site) instead of tracking latest.
Back Up n8n: Volumes, Database, and the Encryption Key
A backup that misses the encryption key is a backup you cannot restore credentials from. Cover all three pieces:
- Database dump on a schedule. This one-liner exports everything n8n stores in Postgres:
cd /opt/n8n docker compose exec postgres pg_dump -U n8n n8n > n8n-backup-$(date +%F).sql - Volume copy for the n8n data folder (
n8n_data), which holds binary files and local settings. Copy it off the server with rsync or your usual backup tool. - The encryption key stored in your password manager, outside the server. Keep at least one copy off the machine.
Test a restore on a throwaway VPS once. A backup you have never restored is a hope, not a plan. The same pattern — volumes, a reverse proxy, and off-server copies — powers our Vaultwarden Docker guide if you want a second app to practice on.
FAQ
Do I have to use Postgres?
No. SQLite is the built-in default and is fine for kicking the tires. Switch to Postgres when workflows carry real work, because it handles concurrent writes and large execution histories far better.
Can I run n8n on a 1 GB VPS?
It will start, but memory gets tight once you run several workflows alongside Postgres. 2 GB of RAM gives comfortable headroom and costs little more on most providers.
Is it safe to expose n8n directly to the internet?
Do not expose port 5678 directly. Always put a reverse proxy with HTTPS in front, keep n8n bound to localhost, and use a strong owner password. n8n holds your API keys, so treat its login page like a production admin panel.
What happens if I lose the encryption key?
Stored credentials become permanently unreadable. Workflows survive, but every credential has to be recreated manually. Keep the key in a password manager from day one.
Does n8n cost anything to self-host?
n8n itself is fair-code licensed and free to run for most uses; check official pricing and licensing if you embed it in a commercial product. Your only recurring cost is the VPS itself, and that varies by provider, so check current pricing before you size the server.
Keeping Your Self-Host n8n VPS Docker Install Healthy
You now have n8n running on your own VPS: Docker Compose with Postgres, HTTPS through a reverse proxy, encrypted credentials, a working test workflow, and a backup routine. The whole footprint is two containers, one compose file, and a password manager entry. Set a monthly reminder to pull updates and verify the backup files exist. Your automations live on hardware you control now, and that is the entire point of self-hosting. Running a file sync on the same box instead? Our Nextcloud Docker Compose guide uses the same layout.