How to Self-Host n8n on Hostinger VPS KVM 2 (Docker, PostgreSQL 16 & Auto SSL)
Most quick-start tutorials leave you with a fragile SQLite container that crashes under webhook spikes or fills up your disk within two months. This guide walks through a clean, hardened Docker Compose deployment built for real production workloads.
TL;DR - When to Use This Setup
- Target hardware: Hostinger VPS KVM 2 (2 vCPU, 8 GB RAM, 100 GB NVMe, ~$8.99-$9.99/mo promotional rate) running Ubuntu 24.04 LTS.
- Software stack: Caddy 2 (automatic Let's Encrypt TLS) + n8n latest + PostgreSQL 16 + automated execution pruning + daily
pg_dumpcron backups. - Prefer zero Linux maintenance? If you run fewer than 2,500 executions/month, read our n8n Cloud vs Self-Hosted TCO analysis before provisioning a server.
1. Choosing the Right VPS Tier (Why 8 GB RAM Matters)
n8n executes workflows inside a Node.js runtime. While an idle n8n container consumes only ~250-350 MB of RAM, memory usage climbs sharply as soon as a workflow parses large JSON arrays, processes binary attachments (PDFs, images, audio), or invokes AI agent chains.
| VPS Tier | Specs | Safe Workload Profile |
|---|---|---|
| 1-2 GB RAM VPS | 1 vCPU / 1-2 GB | Light webhook relays only; high risk of Linux OOM kills (exit code 137) on batch jobs. |
| Hostinger KVM 2 | 2 vCPU / 8 GB / 100 GB NVMe | Sweet spot for n8n + PostgreSQL 16 + Caddy + Redis queue workers & AI workflows. |
| 4 vCPU / 16 GB+ | 4 vCPU / 16 GB | High-concurrency webhook ingestion (50+ parallel executions) or local headless browsers. |
Note: Hostinger also offers a 1-click Ubuntu + n8n OS template inside hPanel, but setting up clean Docker Compose files yourself (Steps 2-4 below) gives you full control over PostgreSQL, log pruning, and backups.
2. Provision Ubuntu 24.04, Install Docker & Lock Down UFW
After deploying an Ubuntu 24.04 LTS instance, SSH into your server and install the official Docker Engine alongside UFW firewall rules. Never expose port 5678 directly on 0.0.0.0 - all external traffic should pass strictly through Caddy on port 443.
# 1. Update packages and install Docker + Compose plugin
sudo apt update && sudo apt upgrade -y
curl -fsSL https://get.docker.com | sudo sh
# 2. Configure UFW firewall (allow SSH, HTTP, HTTPS only)
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
# 3. Optional: add a 2 GB swap file as extra OOM safety insurance
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile && sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab 3. Point Your Subdomain DNS Cleanly
Open your domain's DNS dashboard (for example, Cloudflare DNS or Porkbun) and create an A record:
- Type:
A - Name:
n8n(forn8n.yourdomain.com) - Value: Your VPS public IPv4 address
- Cloudflare Proxy status: Set to DNS Only (grey cloud) so Caddy can verify Let's Encrypt certificates directly without redirect loops.
4. Deploy Hardened Docker Compose (PostgreSQL 16 + Auto Pruning)
You can customize this configuration visually with our n8n Docker Compose Generator, or create the three files directly inside /opt/n8n. Notice two production settings most tutorials omit:
EXECUTIONS_DATA_PRUNE=trueprevents old execution logs from silently filling your 100 GB NVMe disk.flush_interval -1inside Caddy ensures Server-Sent Events (SSE) and real-time UI execution updates never buffer or hang.
File 1: /opt/n8n/.env (chmod 600 .env)
N8N_HOST=n8n.yourdomain.com
WEBHOOK_URL=https://n8n.yourdomain.com/
GENERIC_TIMEZONE=America/New_York
# Generate with: openssl rand -hex 32
N8N_ENCRYPTION_KEY=replace_with_64_char_random_hex_key
POSTGRES_USER=n8n
# Generate with: openssl rand -hex 20
POSTGRES_PASSWORD=replace_with_strong_random_password
POSTGRES_DB=n8n File 2: /opt/n8n/Caddyfile
n8n.yourdomain.com {
reverse_proxy n8n:5678 {
flush_interval -1
}
} File 3: /opt/n8n/docker-compose.yml
services:
caddy:
image: caddy:2-alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy_data:/data
- caddy_config:/config
depends_on:
- n8n
postgres:
image: postgres:16-alpine
restart: unless-stopped
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 10
n8n:
image: docker.n8n.io/n8nio/n8n:latest
restart: unless-stopped
environment:
- N8N_HOST=${N8N_HOST}
- N8N_PROTOCOL=https
- WEBHOOK_URL=${WEBHOOK_URL}
- N8N_PROXY_HOPS=1
- GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
- DB_POSTGRESDB_USER=${POSTGRES_USER}
- DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
- EXECUTIONS_DATA_PRUNE=true
- EXECUTIONS_DATA_MAX_AGE=168
- EXECUTIONS_DATA_PRUNE_MAX_COUNT=25000
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
volumes:
n8n_data:
postgres_data:
caddy_data:
caddy_config: Start the stack and verify that Caddy provisions your certificate cleanly:
cd /opt/n8n
chmod 600 .env
sudo docker compose up -d
sudo docker compose logs -f caddy n8n 5. Set Up Automated Daily PostgreSQL Backups
Self-hosting only saves money if a bad upgrade never wipes out your credentials or workflows. Add a lightweight nightly backup script at /opt/n8n/backup.sh and keep a copy of your N8N_ENCRYPTION_KEY in your password manager (without that key, stored OAuth/API credentials inside PostgreSQL cannot be decrypted on a new server).
#!/usr/bin/env bash
set -euo pipefail
BACKUP_DIR="/opt/n8n/backups"
STAMP=$(date +"%Y-%m-%d_%H%M")
mkdir -p "$BACKUP_DIR"
# Dump PostgreSQL database cleanly without stopping n8n
docker compose -f /opt/n8n/docker-compose.yml exec -T postgres \
pg_dump -U n8n -d n8n | gzip > "$BACKUP_DIR/n8n_db_$STAMP.sql.gz"
# Retain the last 14 days of local snapshots
find "$BACKUP_DIR" -type f -name "*.sql.gz" -mtime +14 -delete
Make it executable (chmod +x /opt/n8n/backup.sh) and schedule via root crontab: 15 3 * * * /opt/n8n/backup.sh.
6. Zero-Surprise Upgrades & Common Webhook Fixes
Always trigger /opt/n8n/backup.sh before pulling a new release, or pin minor versions (e.g., docker.n8n.io/n8nio/n8n:1.88.0) if you run revenue-critical workflows:
cd /opt/n8n
./backup.sh
sudo docker compose pull n8n
sudo docker compose up -d Fix: Webhook URLs still show http://localhost:5678 v
Ensure WEBHOOK_URL=https://n8n.yourdomain.com/ (including the trailing slash) and N8N_PROTOCOL=https are present in your n8n container environment, then recreate the container with sudo docker compose up -d --force-recreate n8n.
Fix: OAuth callbacks fail or Express rate-limit throws X-Forwarded-For warnings v
Add N8N_PROXY_HOPS=1 to the environment block so n8n trusts the single Caddy reverse proxy layer sitting in front of port 5678.
Fix: Container restarts with Exit Code 137 on heavy JSON/AI workflows v
Exit code 137 means the Linux kernel OOM killer terminated Node.js. Upgrade to an 8 GB RAM instance (such as Hostinger KVM 2), split batch loops into smaller chunks (e.g., 50 items per batch), and set NODE_OPTIONS=--max-old-space-size=4096.