2026-11-23 · AgentStack Research Desk

Self-Hosting n8n with Docker, PostgreSQL & Caddy on a Low-Cost VPS (2026 Production Blueprint)

#devops#docker#ai#tutorial
Self-Hosting n8n with Docker, PostgreSQL & Caddy on a Low-Cost VPS (2026 Production Blueprint)
Key Takeaway: Self-hosting n8n with Docker Compose, PostgreSQL 16, and Caddy on a $6–$9/month KVM VPS removes per-execution billing entirely and avoids the $15–$45/month metered compute creep of PaaS hosts for always-on AI agent workflows.
Transparency note: Some links in this article are affiliate links. If you sign up through them, AgentStack may earn a commission at no extra cost to you. We only recommend tools we evaluate rigorously.

Quick Verdict (TL;DR)

Most developers start automating AI agents with cloud plans and hit a wall once multi-step LLM chains run every few minutes. Self-hosting n8n Community Edition flips your unit economics from variable execution billing to a flat monthly server bill.

Below is the exact production blueprint we recommend in 2026: n8n + PostgreSQL 16 + Caddy (automatic Let's Encrypt TLS) locked down behind UFW.

Side-by-Side Comparison

Option Monthly Cost (24/7) Execution Limit Database & SSL Ops Overhead Best For
Hostinger VPS KVM 1 (1 vCPU / 4 GB) ~$6.49/mo promo Unlimited (CPU bound) Self-managed Docker Low–Medium Solo builders & light AI workflows
Hostinger VPS KVM 2 (2 vCPU / 8 GB) ~$8.79/mo promo Unlimited (CPU bound) Postgres + Redis + Caddy Low–Medium Production AI agents & agencies
Railway (Metered Hobby/Pro) ~$15–$45+/mo always-on Unlimited (metered CPU/RAM) Managed Postgres + HTTPS Very Low Devs wanting Git-push zero-config
n8n Cloud Starter ~€20–€24/mo 2,500 executions/mo Fully managed Zero Teams avoiding server maintenance

Why PostgreSQL + Caddy Beats Default Quickstarts

Many tutorials tell you to run docker run n8nio/n8n with default SQLite and expose port 5678 directly. That breaks quickly in production for three reasons:

  1. SQLite write locks: Concurrent webhooks and AI agent memory writes contend on a single file.
  2. Webhook TLS failures: Slack, Stripe, Telegram, and OpenAI callbacks require valid HTTPS certificates.
  3. Exposed admin ports: Leaving 0.0.0.0:5678 open to the public internet invites brute-force scans.

By binding n8n strictly to 127.0.0.1:5678 inside Docker and placing Caddy in front, you get automatic Let's Encrypt TLS provisioning and zero-downtime certificate renewal with a 15-line Caddyfile.

Step 1: Prepare Your Ubuntu VPS & Directories

Provision an Ubuntu 24.04 KVM VPS (we recommend 2 vCPU / 8 GB RAM on Hostinger VPS if you plan to run PostgreSQL alongside heavy AI workflows). Point an A record such as n8n.yourdomain.com to your server's public IPv4 address.

SSH into the server, install Docker Engine + Compose plugin, and create your isolated directory layout:

sudo mkdir -p /opt/n8n
cd /opt/n8n

Create a .env file (chmod 600 .env) with strong credentials:

DOMAIN_NAME=n8n.yourdomain.com
SSL_EMAIL=admin@yourdomain.com
POSTGRES_USER=n8nuser
POSTGRES_PASSWORD=ReplaceWithLongRandomPassword2026!
POSTGRES_DB=n8n
N8N_ENCRYPTION_KEY=ReplaceWith32CharRandomHexKey
GENERIC_TIMEZONE=UTC
Keep a backup copy of N8N_ENCRYPTION_KEY in your password manager—n8n uses it to encrypt all stored API credentials inside PostgreSQL.

Step 2: Production docker-compose.yml

Save the following file at /opt/n8n/docker-compose.yml:

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgresql_data:/var/lib/postgresql/data
    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:latest
    restart: unless-stopped
    environment:
      - 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}
      - N8N_HOST=${DOMAIN_NAME}
      - WEBHOOK_URL=https://${DOMAIN_NAME}/
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
      - EXECUTIONS_DATA_PRUNE=true
      - EXECUTIONS_DATA_MAX_AGE=168
      - NODE_ENV=production
    ports:
      - "127.0.0.1:5678:5678"
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy

  caddy:
    image: caddy:alpine
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - n8n

volumes:
  postgresql_data:
  n8n_data:
  caddy_data:
  caddy_config:

Notice EXECUTIONS_DATA_PRUNE=true and EXECUTIONS_DATA_MAX_AGE=168 (7 days). Without automatic execution pruning, high-frequency AI workflows will silently fill your disk with JSON logs within a few weeks.

Step 3: Automatic HTTPS with Caddyfile

Create /opt/n8n/Caddyfile:

{
    email {$SSL_EMAIL}
}

{$DOMAIN_NAME} {
    reverse_proxy n8n:5678 {
        header_up Host {host}
        header_up X-Real-IP {remote_host}
        header_up X-Forwarded-Proto {scheme}
        transport http {
            dial_timeout 30s
            response_header_timeout 300s
        }
    }
}

The response_header_timeout 300s directive is critical for AI agent webhooks that wait on multi-step LLM reasoning calls before returning an HTTP response.

Step 4: Lock Down UFW Firewall & Launch

Never leave database or raw app ports exposed. Allow only SSH, HTTP, and HTTPS:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Start the stack and verify Caddy obtains your TLS certificate:

docker compose up -d
docker compose logs -f caddy

Within 15 to 30 seconds, open https://n8n.yourdomain.com in your browser to create your owner account.

Automated Nightly PostgreSQL Backups

Before trusting your self-hosted instance with production workflows, add a one-line cron job (crontab -e) that dumps PostgreSQL every night at 03:00 UTC:

0 3 * * * docker exec $(docker compose -f /opt/n8n/docker-compose.yml ps -q postgres) pg_dump -U n8nuser n8n | gzip > /opt/n8n/backup_$(date +\%F).sql.gz && find /opt/n8n -name 'backup_*.sql.gz' -mtime +7 -delete

Who Should Choose What?

Tool Starting Price Best For Official Link
Hostinger VPS $4.99/mo Low-cost KVM VPS for self-hosting n8n, Ollama, and autonomous AI agent workers Get Hostinger VPS
n8n Cloud Self-hosted Free / €20/mo Cloud Developers building custom LangChain & AI agent pipelines with full code control Start with n8n
Railway $5/mo usage-based Deploying Python AI agents, n8n instances, and APIs in under 60 seconds Deploy on Railway
Systeme.io Free / $27/mo Solopreneurs replacing multiple marketing SaaS subscriptions with one funnel + email stack Launch Free on Systeme.io

Frequently Asked Questions

What VPS specs do I need to self-host n8n with PostgreSQL in 2026?

A 1 vCPU / 4 GB RAM VPS works for light personal workflows, while a 2 vCPU / 8 GB RAM KVM VPS (such as Hostinger KVM 2) is the recommended sweet spot for running n8n, PostgreSQL 16, and Caddy smoothly under concurrent AI agent webhooks.

Is self-hosted n8n really free for unlimited executions?

Yes. The n8n Community Edition software has no artificial execution cap when self-hosted. Your only limit is your VPS CPU, RAM, and disk I/O.

Why does Railway cost more than a VPS for always-on n8n?

Railway bills continuously for metered CPU and RAM usage. Because n8n with webhooks and cron jobs must stay online 24/7 alongside a database, metered compute typically lands between $15 and $45+ per month compared to a flat $6–$9 KVM VPS.

How do I update self-hosted n8n safely without losing workflows?

Back up PostgreSQL first, then run docker compose pull && docker compose up -d inside /opt/n8n. Because workflow definitions live in the persistent PostgreSQL volume and n8n_data, upgrades preserve all workflows and credentials.