Blog
Automationn8nSelf-HostingDocker

How to Self-Host n8n in 2026: Docker Compose + PostgreSQL Setup (Step by Step)

Step-by-step guide to self-hosting n8n with Docker Compose and PostgreSQL: HTTPS, encryption key, backups, safe updates and when to use queue mode.

Ameer HamzaNovember 13, 20269 min read
How to Self-Host n8n in 2026: Docker Compose + Postgres Guide

n8n has become the go-to automation platform for teams that want AI agents, custom code and full control over their data. The part that makes it different from Make.com or Zapier is also the part most guides rush through: you can run it on your own server, with no per-execution fees and no workflow data leaving your infrastructure.

This guide walks through a production-ready self-hosted n8n setup in 2026: Docker Compose, PostgreSQL instead of the default SQLite, HTTPS through a reverse proxy, backups, safe updates and when to switch on queue mode. It follows n8n's own official Docker Compose example, with the settings that example leaves out for a real public deployment.

I run client workflows on n8n, from a Reddit client-hunting system that routes leads into Slack to an AI comment-response bot. If you're still choosing a platform, start with my Make.com vs n8n comparison, then come back here.

Should You Self-Host n8n?

Self-hosting is the right call when:

  • You run high volume. Self-hosted n8n has no per-execution fee, so cost tracks your server, not your workflow count.
  • Data has to stay with you. Customer records, internal documents or regulated data never pass through a third-party automation cloud.
  • You want local or private AI models. Agent workflows that call a model on your own hardware need n8n running next to it.
  • You need custom code and packages. JavaScript and Python code nodes run inside your own environment.

n8n Cloud (or Make.com) is the better choice when nobody on the team wants to own a server, apply updates or restore a backup at 2 a.m. Self-hosting trades a monthly plan for responsibility, and that trade only pays off if someone actually takes it on.

What You Need Before You Start

  • A Linux VPS. 2 vCPU and 2 to 4 GB of RAM is a comfortable starting point for a small team; code-heavy and AI workflows want the upper end.
  • A domain or subdomain, such as n8n.yourcompany.com, pointed at the server's IP with an A record.
  • Docker with the Compose plugin installed on the server.
  • About 30 minutes.

Step 1: Create the Project Folder and .env File

Create a folder (for example ~/n8n) and add a .env file. Docker Compose reads it automatically, which keeps every secret out of the compose file itself:

# n8n version (pin it; upgrade on purpose, not by accident)
N8N_VERSION=2.47.4

# PostgreSQL superuser, used only to create the database
POSTGRES_USER=n8n_admin
POSTGRES_PASSWORD=replace-with-a-long-random-password
POSTGRES_DB=n8n

# Least-privilege user that n8n actually connects with
POSTGRES_NON_ROOT_USER=n8n_app
POSTGRES_NON_ROOT_PASSWORD=replace-with-another-long-random-password

# Shared secret between n8n and its task runner
RUNNERS_AUTH_TOKEN=replace-with-a-random-token

# Encrypts saved credentials. Generate once, back it up, never change it.
N8N_ENCRYPTION_KEY=replace-with-openssl-rand-hex-32

# Your public address
N8N_HOST=n8n.example.com
GENERIC_TIMEZONE=Asia/Karachi

Two of these deserve extra care. N8N_ENCRYPTION_KEY encrypts every credential you save in n8n. If you don't set it, n8n generates one on first launch inside its data volume, which is easy to lose during a migration. Set it yourself and store a copy outside the server. And pin N8N_VERSION instead of using latest, so an unplanned restart never upgrades you by surprise.

Step 2: Add the Database Init Script

n8n should not connect to Postgres as the superuser. This script runs once when the database volume is first created and sets up a limited user for n8n. Save it as init-data.sh in the same folder:

#!/bin/bash
set -e

# Runs once, on the first start of an empty Postgres volume
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
  CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}';
  GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER};
  GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER};
EOSQL

Make it executable with chmod +x init-data.sh.

Step 3: Write docker-compose.yml

This is n8n's official Postgres example plus the settings a public deployment needs: the encryption key, your hostname, the webhook URL, proxy awareness and a timezone. It runs three containers: PostgreSQL, n8n itself and n8n's task runner, which executes Code-node JavaScript and Python in a separate process.

volumes:
  db_storage:
  n8n_storage:

services:
  postgres:
    image: postgres:18
    restart: always
    environment:
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_DB
      - POSTGRES_NON_ROOT_USER
      - POSTGRES_NON_ROOT_PASSWORD
      - PGDATA=/var/lib/postgresql/data
    volumes:
      - db_storage:/var/lib/postgresql/data
      - ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
      interval: 5s
      timeout: 5s
      retries: 10

  n8n:
    image: docker.n8n.io/n8nio/n8n:${N8N_VERSION}
    restart: always
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - N8N_HOST=${N8N_HOST}
      - N8N_PROTOCOL=https
      - N8N_WEBHOOK_URL=https://${N8N_HOST}/
      - N8N_PROXY_HOPS=1
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
      - N8N_RUNNERS_MODE=external
      - N8N_RUNNERS_AUTH_TOKEN=${RUNNERS_AUTH_TOKEN}
      - N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0
    ports:
      - 127.0.0.1:5678:5678
    volumes:
      - n8n_storage:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy

  n8n-runner:
    image: n8nio/runners:${N8N_VERSION}
    restart: always
    environment:
      - N8N_RUNNERS_AUTH_TOKEN=${RUNNERS_AUTH_TOKEN}
      - N8N_RUNNERS_TASK_BROKER_URI=http://n8n:5679
    depends_on:
      - n8n

A few details that matter:

  • DB_TYPE=postgresdb, not postgres. n8n defaults to SQLite, so a wrong value here is easy to miss.
  • N8N_WEBHOOK_URL tells n8n the public address to put in webhook URLs. It replaced the older WEBHOOK_URL variable, which is deprecated from n8n 2.35.0.
  • 127.0.0.1:5678:5678 publishes n8n only on the server itself. The reverse proxy in Step 5 is the only way in from the internet.
  • N8N_PROXY_HOPS=1 tells n8n it sits behind one reverse proxy, so it reads the real client address correctly.

Step 4: Start n8n

# Generate the secrets for .env
openssl rand -hex 32   # use for N8N_ENCRYPTION_KEY
openssl rand -hex 24   # use for passwords and RUNNERS_AUTH_TOKEN

# Start everything in the background
docker compose up -d

# Watch n8n boot; stop with Ctrl+C
docker compose logs -f n8n

When the logs show the editor is accessible, n8n is running. It isn't reachable from your browser yet, because port 5678 is bound to localhost on purpose.

Step 5: Put n8n Behind HTTPS

Webhooks from Stripe, Slack, GitHub and most other services require HTTPS, and your n8n login should never travel over plain HTTP. The quickest route is Caddy, which requests and renews a Let's Encrypt certificate automatically. Install Caddy on the server and use this as the whole Caddyfile:

n8n.example.com {
  reverse_proxy localhost:5678
}

Reload Caddy, open https://n8n.example.com and create the owner account. Nginx or Traefik work just as well if you already run one of them; the only requirement is that it forwards traffic to localhost:5678.

Create the owner account immediately after the first start. Until you do, anyone who reaches the URL first can claim the instance.

Step 6: Back Up What Actually Matters

Two things make your n8n instance recoverable: the Postgres database (workflows, credentials and execution history) and the encryption key. Lose the key and every saved credential becomes unreadable, even with a perfect database backup.

# Nightly database dump (run from the folder with docker-compose.yml)
docker compose exec -T postgres pg_dump -U n8n_admin -d n8n > n8n-$(date +%F).sql

# The encryption key lives in .env: keep a copy somewhere safe, off the server

Schedule the dump with cron, copy the files off the server (object storage or another machine), and test a restore once before you need it.

Updating n8n Safely

# 1. Back up first (see above)
# 2. Change N8N_VERSION in .env to the new release
# 3. Pull and restart
docker compose pull
docker compose up -d

Read the release notes before jumping several versions at once, and update the n8n and runner images together; the compose file above already ties both to N8N_VERSION.

When to Switch On Queue Mode

A single n8n instance handles more than most small teams ever send it. Once executions start queuing up, or one heavy workflow slows down everything else, queue mode splits the work: the main instance receives triggers and webhooks, Redis holds the job queue, and separate worker containers execute the workflows.

# Added to the main n8n service and to every worker
- EXECUTIONS_MODE=queue
- QUEUE_BULL_REDIS_HOST=redis
- QUEUE_BULL_REDIS_PORT=6379
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}   # must match the main instance

# Each worker runs the same image with the "worker" command
command: worker

Queue mode needs Redis plus a database every process can reach; n8n doesn't support a distributed setup on SQLite, which is one more reason to start on Postgres from day one.

Self-Hosted n8n vs n8n Cloud vs Make.com

FeatureSelf-hosted n8nn8n CloudMake.com
HostingYour own serverManaged by n8nManaged by Make (cloud-only)
PricingServer cost, no per-execution feePlan-based on executionsPlan-based on operations
Setup effortHighest: you run the stackLowLowest
Data locationYour infrastructuren8n's cloudMake's cloud
Custom codeJavaScript and Python code nodesJavaScript and Python code nodesBuilt-in functions
Best forHigh volume, data control, private AIn8n without the server workNon-technical teams

Common Mistakes When Self-Hosting n8n

  • Staying on SQLite in production. It works until it doesn't, and migrating a busy instance later is far more painful than starting on Postgres.
  • Not setting N8N_ENCRYPTION_KEY. The auto-generated key lives inside the data volume and disappears with it.
  • Webhook URLs that say localhost. If your Slack or Stripe webhooks point at http://localhost:5678, N8N_WEBHOOK_URL isn't set.
  • Exposing port 5678 to the internet instead of putting n8n behind a reverse proxy with HTTPS.
  • Using the latest tag. An unplanned container restart becomes an unplanned major upgrade.
  • Backups nobody has ever restored. A backup you haven't tested is a guess.

Key Takeaways

  1. Self-host n8n when volume, data control or private AI models matter; otherwise n8n Cloud or Make.com saves you the server work.
  2. Start on PostgreSQL, with a dedicated low-privilege database user.
  3. Set and back up N8N_ENCRYPTION_KEY before you save a single credential.
  4. Keep n8n behind HTTPS and set N8N_WEBHOOK_URL so webhooks use your public address.
  5. Switch on queue mode only when one instance stops keeping up.

Want n8n running without becoming its sysadmin? Setting up, securing and building on self-hosted n8n is part of my workflow automation services, from the first server to the workflows that run on it.

Frequently Asked Questions

Is self-hosted n8n free?
The self-hosted Community edition has no license fee or per-execution charges; you pay for the server it runs on. It's distributed under n8n's Sustainable Use License, and some enterprise features (such as SSO and advanced permissions) require a paid license.
How much RAM does self-hosted n8n need?
A small team can start on a VPS with 2 vCPU and 2 to 4 GB of RAM. Heavy Code-node workloads, large data transformations and AI agent workflows use noticeably more memory, so watch usage after launch and scale up or move to queue mode when it climbs.
Can I self-host n8n with SQLite instead of PostgreSQL?
Yes, SQLite is n8n's default and is fine for testing. For production, PostgreSQL is the better choice: it handles concurrent load better, is easier to back up and is required in practice for queue mode, because n8n doesn't support a distributed setup on SQLite.
What happens if I lose N8N_ENCRYPTION_KEY?
n8n can no longer decrypt the credentials stored in its database, so every saved connection (API keys, OAuth tokens, passwords) has to be re-entered. Your workflows survive, but none of them can authenticate until you do. Back the key up separately from the database.
Do I need queue mode?
Most small and mid-size teams don't. A single instance on PostgreSQL handles a lot of traffic. Queue mode with Redis and workers is worth it when executions start backing up or one heavy workflow slows everything else down.
Can I self-host Make.com instead?
No. Make.com is cloud-only. If self-hosting is a requirement, n8n is the option of the two that supports it.