Home » n8n» How to self-host n8n with Docker and PostgreSQL

How to self-host n8n?

This tutorial explains How to self-host n8n and gives a working Docker Compose setup for a VPS. You can self-host n8n by running the official n8n Docker image with a PostgreSQL database, exposed on port 5678. Follow the steps below for installation, configuration, updates, and basic security guidance.

What You Need

  • A VPS or cloud instance with Ubuntu 20.04+ or similar.
  • Root or sudo access.
  • Docker and Docker Compose installed.
  • Basic DNS pointing to your server (optional but recommended for TLS).
  • At least 1 GB RAM for small workflows; 2+ GB recommended.

How to Install Docker and Docker Compose

Install Docker and Docker Compose with these commands on Ubuntu. Run them as sudo or root.

sudo apt update
sudo apt install -y docker.io docker-compose
sudo systemctl enable --now docker
sudo usermod -aG docker $USER
newgrp docker

Step 1: Create a Docker Compose file

Place this file in a project folder like /opt/n8n. Edit passwords and users before launching.

mkdir -p /opt/n8n
cd /opt/n8n
cat > docker-compose.yml <<'YML'
version: '3.8'
services:
  postgres:
    image: postgres:13
    restart: always
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=CHANGE_ME_DB_PASSWORD
      - POSTGRES_DB=n8n
    volumes:
      - n8n_postgres:/var/lib/postgresql/data
  n8n:
    image: n8nio/n8n:latest
    restart: always
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=CHANGE_ME_DB_PASSWORD
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=admin
      - N8N_BASIC_AUTH_PASSWORD=CHANGE_ME_UI_PASSWORD
      - GENERIC_TIMEZONE=UTC
    depends_on:
      - postgres
    volumes:
      - n8n_data:/home/node/.n8n
volumes:
  n8n_postgres:
  n8n_data:
YML

Step 2: Start n8n and PostgreSQL

Start the services and check logs.

cd /opt/n8n
docker-compose pull
docker-compose up -d
docker-compose logs -f n8n

Step 3: Test the installation

Open http://your-server-ip:5678 in the browser. Use the basic auth user and password you set. Create a simple workflow to verify triggers and nodes.

Step 4: Example backup and update commands

Backup the PostgreSQL database with pg_dump. Update containers safely with pull and recreate.

docker-compose exec postgres pg_dump -U n8n n8n > /opt/n8n/backups/n8n-$(date +%F).sql

# Update n8n and postgres images
cd /opt/n8n
docker-compose pull
docker-compose up -d

Update

To update n8n, pull the latest images and restart the stack. Always back up the database and the n8n_data volume before a major update. Test updates on a staging instance when possible.

# backup example
mkdir -p /opt/n8n/backups
docker-compose exec postgres pg_dump -U n8n n8n > /opt/n8n/backups/n8n-$(date +%F).sql

# update
docker-compose pull
docker-compose up -d

Security

  • Enable N8N_BASIC_AUTH_ACTIVE and set strong credentials.
  • Use a reverse proxy (nginx, Caddy) with TLS (Let's Encrypt) to serve HTTPS.
  • Run a firewall: example with ufw:
sudo apt install -y ufw
sudo ufw allow OpenSSH
sudo ufw allow 443/tcp
sudo ufw allow 80/tcp   # only if using a reverse proxy
sudo ufw enable
  • Do not expose PostgreSQL to the public internet; keep it on the Docker network or private IP.
  • Store secrets in environment variables or a secret manager. Avoid committing them to git.
  • Limit admin access by IP and use strong passwords or OAuth where available.

Done

You now have a working self-hosted n8n instance running on Docker with PostgreSQL. Use the backup and update commands before changes. Secure the instance with HTTPS and strong credentials. Expand by adding a reverse proxy, external storage, or managed DNS for production use.


Neil
Written by Neil

Neil is a true n8n geek who lives and breathes workflow automation. He dives deep into nodes, triggers, webhooks, custom logic, and self-hosting setups, sharing everything he learns about n8n on AutomationCompare.com. As part of a broader team of automation specialists, Neil focuses purely on mastering n8n and helping others unlock its full potential.

Keep Reading

Scroll to Top