OwnServa
Automation · Installation guide

How to self-host n8n with Docker and PostgreSQL

Install self-hosted n8n on Ubuntu with PostgreSQL, a persistent encryption key, HTTPS webhooks and your first owner account.

This guide installs a single n8n automation instance with PostgreSQL, a stable credential-encryption key and an HTTPS address for webhooks. It does not enable queue mode or the optional AI-assistant sandbox stack. An OwnServa starting estimate is 2 CPU cores and 4 GB RAM; workflow concurrency and execution data determine actual capacity.

Before you start

Complete the Docker and HTTPS preparation. Choose automation.example.com and reserve loopback port 5678. Review n8n's license and self-hosting terms for your intended use. Create a directory and generate two different secrets:

mkdir -p ~/selfhosted/n8n
cd ~/selfhosted/n8n
umask 077
openssl rand -hex 32
openssl rand -hex 32

Save as .env, replacing both placeholders with the separate outputs:

DB_PASSWORD=REPLACE_WITH_FIRST_RANDOM_SECRET
N8N_ENCRYPTION_KEY=REPLACE_WITH_SECOND_RANDOM_SECRET
N8N_HOST=automation.example.com
TZ=Africa/Nairobi

The encryption key is not the database password. Keep it unchanged across restarts and store it with your backups; losing it can make stored integration credentials unreadable.

Set up PostgreSQL and n8n

Save as compose.yaml:

services:
  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: n8n
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
    volumes:
      - database:/var/lib/postgresql/data
    healthcheck:
      test: [CMD-SHELL, "pg_isready -U n8n -d n8n"]
      interval: 10s
      timeout: 5s
      retries: 12
  n8n:
    image: docker.n8n.io/n8nio/n8n:stable
    restart: unless-stopped
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: db
      DB_POSTGRESDB_PORT: "5432"
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_USER: n8n
      DB_POSTGRESDB_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY:?set N8N_ENCRYPTION_KEY}
      N8N_HOST: ${N8N_HOST}
      N8N_PROTOCOL: https
      N8N_PORT: "5678"
      N8N_EDITOR_BASE_URL: https://${N8N_HOST}/
      WEBHOOK_URL: https://${N8N_HOST}/
      N8N_PROXY_HOPS: "1"
      GENERIC_TIMEZONE: ${TZ}
      TZ: ${TZ}
      N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"
    volumes:
      - settings:/home/node/.n8n
    depends_on:
      db:
        condition: service_healthy
volumes:
  database:
  settings:

PostgreSQL creates the database on its first start, and n8n applies its own schema. The database has no public port. Keep the .n8n volume even when using PostgreSQL. The one-proxy-hop setting assumes a direct browser → Caddy → n8n route; review it if you add a CDN or other proxy.

Start the editor behind HTTPS

chmod 600 .env
sudo docker compose config --quiet
sudo docker compose up -d
sudo docker compose ps
sudo docker compose logs --tail=80 n8n db

Configure Caddy for automation.example.com to 127.0.0.1:5678 using the HTTPS guide. Use the final HTTPS URL for setup; do not turn off secure cookies just to work around an incorrect public address.

Create your first owner account

Open https://automation.example.com. Complete the initial owner form with your email, name and a strong password, then sign in. This is the n8n instance owner, separate from an n8n Cloud or OwnServa account.

Create a workflow with a manual trigger and a simple data-editing step. Run it manually and confirm a successful execution. Then test a webhook using the URL shown by n8n; test and production webhook URLs have different lifecycles. Enable/publish the workflow as required by your installed release before relying on its production URL. Configure SMTP if you need reliable invitations or password recovery.

Back up credentials and workflows

Preserve a PostgreSQL backup, the settings volume and the exact N8N_ENCRYPTION_KEY. Do not rotate that key by simply changing .env. Workflow exports alone are not a full recovery plan for encrypted credentials. Keep execution retention appropriate for your disk capacity and the data you process.

Before upgrades, read release notes and pin a supported version. This basic setup omits optional assistant/sandbox services; use n8n's full current deployment guide if you enable those features.

Troubleshooting

  • Secure-cookie warning: use the configured HTTPS address and check the proxy scheme.
  • Webhook URLs contain localhost: check WEBHOOK_URL, N8N_HOST and your final public domain, then recreate the service.
  • Credentials stop decrypting after a move: restore the original encryption key; do not keep generating new keys.
  • Connection refused to PostgreSQL: check database health and the service hostname db.

Official references

See n8n's Compose installation, database configuration, database environment variables and deployment environment variables.

Written by OwnServa. Check the official release notes before updating, keep independent backups and test a restore before relying on your installation.

Get it done for you.

Want a hand setting up n8n? Create an OwnServa account and tell us what you want to run. We can discuss the server, installation and first-time setup with you.

We’ll confirm the scope, availability and any costs before work begins.