Self-host n8n on a VPS with Docker, Postgres and HTTPS

To self-host n8n you need a Linux VPS with a public IPv4 address, a subdomain, and four containers: n8n, its task runner, Postgres and a reverse proxy that handles HTTPS. Two settings decide whether it keeps working: the public webhook URL and the encryption key.

Updated: · Prices in the text are with the public IP (₼10 a month) unless stated otherwise.

What to prepare before installing

ItemWhy
2 vCPU and 4 GB RAMComfortable starting point for n8n and Postgres; 2 GB works for a few light workflows
Public IPv4Webhooks, OAuth callbacks and your SSH session arrive on it; one address, ₼10 a month
A record, e.g. n8n.example.comCertificates and OAuth callbacks need a name
Docker Engine with ComposeInstall it from the official repository, see Docker on a VPS
Ports 22, 80 and 443 open80 is used to issue and renew the certificate
  • Basic₼27.90/mo

    2 vCPU · 2 GB RAM · 40 GB NVMe

    Public IP +₼10/mo, With public IP: ₼37.90/mo

  • Standard₼39.90/mo

    2 vCPU · 4 GB RAM · 60 GB NVMe

    Public IP +₼10/mo, With public IP: ₼49.90/mo

  • Professional₼73.90/mo

    4 vCPU · 8 GB RAM · 100 GB NVMe

    Public IP +₼10/mo, With public IP: ₼83.90/mo

Prices in manat (AZN), billed monthly. The public IP is +₼10 a month and is assigned automatically.

With public IP, Standard is ₼49.90 a month; Basic with 2 GB is ₼37.90. Memory is used mostly by workflows that load large files or thousands of rows in one run, so split big jobs into batches before you pay for more RAM. If the domain has no zone yet, DNS management comes free with any Cloudraft service.

The .env file

Create /opt/n8n/.env. Generate each secret separately and replace the placeholders:

sudo mkdir -p /opt/n8n && cd /opt/n8n
openssl rand -hex 32   # run three times: database, encryption key, runner token
# /opt/n8n/.env
N8N_VERSION=2.48.8
N8N_DOMAIN=n8n.example.com
TZ=Asia/Baku
POSTGRES_PASSWORD=put_the_first_value_here
N8N_ENCRYPTION_KEY=put_the_second_value_here
RUNNERS_AUTH_TOKEN=put_the_third_value_here

Protect it with sudo chmod 600 .env. Version 2.48.8 was the stable release when this page was updated; check n8n's release list and write the current stable number.

docker-compose file with Postgres

n8n uses SQLite by default. Postgres is the better choice for anything the company depends on: it copes with many simultaneous executions and has proper dump tools.

# /opt/n8n/compose.yaml
services:
  postgres:
    image: postgres:17-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: n8n
    volumes:
      - db:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
      interval: 10s
      retries: 5

  n8n:
    image: docker.n8n.io/n8nio/n8n:${N8N_VERSION}
    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_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_HOST: ${N8N_DOMAIN}
      N8N_PROTOCOL: https
      N8N_WEBHOOK_URL: https://${N8N_DOMAIN}/
      N8N_PROXY_HOPS: 1
      N8N_RUNNERS_MODE: external
      N8N_RUNNERS_AUTH_TOKEN: ${RUNNERS_AUTH_TOKEN}
      N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
      EXECUTIONS_DATA_PRUNE: "true"
      EXECUTIONS_DATA_MAX_AGE: 336
      GENERIC_TIMEZONE: ${TZ}
      TZ: ${TZ}
    volumes:
      - n8n:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy

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

  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy:/data

volumes:
  db:
  n8n:
  caddy:

Only Caddy publishes ports; n8n and Postgres stay on the internal network. The Caddyfile next to it is four lines:

n8n.example.com {
    reverse_proxy n8n:5678 {
        flush_interval -1
    }
}

Start it with sudo docker compose up -d, watch sudo docker compose logs -f n8n, then open the domain at once and create the owner account. Until you do, anyone who finds the address can claim it. If nginx already runs on this server, use it as the proxy instead: publish n8n on 127.0.0.1:5678, pass the WebSocket upgrade headers, and follow the nginx certificate guide.

Webhook URL and encryption key

N8N_WEBHOOK_URL is the address n8n shows in webhook nodes and registers with outside services. Without it you get links with localhost or port 5678 that nobody can call. Older guides call it WEBHOOK_URL; that name still works but has been deprecated since n8n 2.35. N8N_PROXY_HOPS: 1 tells n8n to trust one proxy.

N8N_ENCRYPTION_KEY encrypts every saved credential. If you move the database to another server without the same key, the workflows arrive but the passwords and tokens inside them cannot be read. Keep a copy in a password manager, not only on the server.

Updating without losing workflows

  1. Read the release notes between your version and the new one, especially for a new major version.
  2. Dump the database: sudo docker compose exec -T postgres pg_dump -U n8n -Fc n8n > ~/n8n-$(date +%F).dump
  3. Change N8N_VERSION in .env. The n8n and runner images must have the same version.
  4. Run sudo docker compose pull && sudo docker compose up -d and test one workflow with a webhook.
  5. If it fails, put the old version back and restore the dump: a newer version may already have changed the database schema.

Copy the dump, .env and Compose file off the server every night; VPS backup to S3 shows how. Disk growth comes mainly from saved execution data, which the prune settings above limit to 14 days.

Licence: what you may do

n8n is fair-code, not open source. Under the Sustainable Use License you can run the community edition free of charge for your own internal business purposes, and consultants may set it up for a client. Hosting n8n and charging other people for access to it is not allowed without an agreement with n8n, and some features need a paid key. Read the licence text before you build a product on it.

n8n hosting questions

How much RAM does n8n need?

Start with 4 GB for n8n, the task runner and Postgres. 2 GB can run a handful of light workflows; large files and big data sets need more than the number of workflows suggests.

Can I run n8n without a domain?

For testing, yes, through an SSH tunnel. Webhooks and OAuth logins expect an HTTPS address that other services can reach, so production needs a domain.

Is self-hosted n8n free?

The community edition has no licence fee for internal use. You pay for the server and your own time for updates and backups.

Should I move from SQLite to Postgres later?

You can, but it means exporting workflows and credentials. If you already expect several users or frequent runs, start with Postgres.

Why do my webhooks show localhost?

The public URL is not set. Add N8N_WEBHOOK_URL with your HTTPS domain, recreate the container, then open the webhook node again and copy the new address.

Read next

All VPS pages

This page in other languages: Azərbaycanca · Русский · Türkçe

Servers in Baku, prices in manat

Create an account, top up your balance and order the plan you need.