n8n Setup: Run n8n Locally with Docker and Postgres
Overview & Architecture
This blueprint runs n8n on your own computer or home server with Docker Compose. Instead of n8n’s default SQLite file, n8n stores its workflows, credentials and executions in PostgreSQL. There is no n8n workflow to import here: the build is the docker-compose.yml and .env files themselves.
Two services run from one compose file:
- postgres (
postgres:16-alpine) with thepgdatavolume and apg_isreadyhealth check. Its port is not published to the host. - n8n (
n8nio/n8n:1.100.1) on port5678, with then8n_datavolume and a health check on/healthz. It waits until PostgreSQL is healthy before starting.
Both services read their settings from the same .env file.
The Pipeline
- Write
docker-compose.yml: the two services, their volumes and health checks. - Write
.env: database name, user and password, the n8n encryption key, and n8n’sDB_TYPE=postgresdb/DB_POSTGRESDB_*settings. - Validate:
docker compose config --quiet && echo ok. - Start:
docker compose up -dcreates the network, both volumes and both containers. - Check health:
docker compose psshowed both containers(healthy)after about a minute. - Prove it:
docker compose exec n8n wget -qO- http://localhost:5678/healthzanswers, and apsqlquery in the postgres container returnedn8n|36: 36 tables n8n created in its PostgreSQL database. - Open n8n at
http://localhost:5678and create the owner account.
What You Need
- Docker with the Docker Compose plugin (
docker compose), on Linux, macOS or Windows - Port 5678 free on the host
- An empty project folder
- Tested with n8n
1.100.1and PostgreSQL16-alpine
The .env file used in the video (placeholder values; replace every change-me before the first start and never commit this file):
POSTGRES_DB=n8n
POSTGRES_USER=n8n
POSTGRES_PASSWORD=change-me
N8N_ENCRYPTION_KEY=change-me-random-key
DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=postgres
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_USER=n8n
DB_POSTGRESDB_PASSWORD=change-me
Key Deployment Hurdles & Solutions
- Start Order: n8n uses
depends_onwithcondition: service_healthy, so it only starts afterpg_isreadypasses. - Matching Database Settings:
DB_POSTGRESDB_*must match thePOSTGRES_*values. They are written out twice in.env, so change both together. - Service Names, Not localhost: n8n reaches the database at
postgres, the compose service name. - Persistent Volumes:
pgdataandn8n_datakeep your data throughdocker compose down.docker compose down -vdeletes them. - Encryption Key: keep
N8N_ENCRYPTION_KEYfixed and backed up, or saved credentials can no longer be decrypted. - First-Start Password: PostgreSQL only applies
POSTGRES_PASSWORDwhen its volume is first created, so set real values before the firstup -d.
Download the docker-compose.yml
The exact compose file from the video. Save it in an empty folder as docker-compose.yml, create the .env file above next to it, then run docker compose up -d.
Download the Companion System Prompt (.md)
Enter your first name to personalize the setup assistant prompt so your AI helper can walk you through Docker Compose, PostgreSQL and n8n step by step and help with any errors. Add your email optionally to get notified when new home lab blueprints drop.