n8n with PostgreSQL: setup, schema and Docker Compose
By Siam Ahmed, software engineer · 6 min read · Updated
n8n stores workflows, credentials and execution history in SQLite by default. For a busy or production server, PostgreSQL is the better choice. This guide shows the environment variables, including the Postgres schema setting, a Docker Compose example, and how to move existing workflows across.
Why use PostgreSQL with n8n
- Handles many executions and parallel workflows without locking.
- Required for queue mode with workers.
- Easy backups with
pg_dump.
n8n Postgres environment variables
| Variable | Example |
|---|---|
DB_TYPE | postgresdb |
DB_POSTGRESDB_HOST | postgres |
DB_POSTGRESDB_PORT | 5432 |
DB_POSTGRESDB_DATABASE | n8n |
DB_POSTGRESDB_USER | n8n |
DB_POSTGRESDB_PASSWORD | a long random password |
DB_POSTGRESDB_SCHEMA | public (default) |
Check the n8n database variables page for SSL and pool options.
The schema setting
DB_POSTGRESDB_SCHEMA tells n8n which Postgres schema to create its tables in. Leave it as public for a database used only by n8n. If the database is shared with other apps, create a schema such as n8n, give the n8n user rights on it, and set the variable to that name.
CREATE SCHEMA n8n AUTHORIZATION n8n;
Docker Compose example
services:
postgres:
image: postgres:16
restart: always
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: change-me
POSTGRES_DB: n8n
volumes:
- pg_data:/var/lib/postgresql/data
n8n:
image: docker.n8n.io/n8nio/n8n
restart: always
ports:
- "127.0.0.1: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
volumes:
- n8n_data:/home/node/.n8n
depends_on:
- postgres
volumes:
pg_data:
n8n_data:
Put HTTPS in front of n8n as shown in how to self-host n8n, and keep the /home/node/.n8n volume: it holds the encryption key for your credentials.
Move from SQLite
- Export:
n8n export:workflow --all --output=workflows.jsonandn8n export:credentials --all --output=creds.json. - Switch the environment variables to Postgres and restart n8n.
- Import:
n8n import:workflow --input=workflows.jsonandn8n import:credentials --input=creds.json. Keep the same encryption key so credentials still decrypt.
Backups and tuning
- Back up nightly with
pg_dump; our workflow backup template adds a second copy. - Prune old executions so the database stays small.
- Size the server with the n8n server size calculator.
Prefer not to manage it? Our n8n hosting plans come set up for you, first month $1.
Questions
Which database does n8n use by default?
SQLite. For production, n8n recommends PostgreSQL, which handles more executions and concurrent workflows better.
How do I connect n8n to PostgreSQL?
Set DB_TYPE=postgresdb plus DB_POSTGRESDB_HOST, DB_POSTGRESDB_PORT, DB_POSTGRESDB_DATABASE, DB_POSTGRESDB_USER and DB_POSTGRESDB_PASSWORD in n8n's environment, then restart n8n.
What is DB_POSTGRESDB_SCHEMA?
The Postgres schema n8n creates its tables in. It defaults to public. Set it if you want n8n's tables in their own schema, for example n8n.
Will switching to Postgres move my workflows?
No. A new database starts empty. Export workflows and credentials first (n8n export:workflow and export:credentials), then import them after the switch.
Do I need Postgres for queue mode?
Queue mode needs Redis and a shared database, and Postgres is the database to use for it. See our n8n queue mode guide.
More guides
$1 VPS First Month: What You Really Pay
$1 VPS hosting for the first month: who qualifies, the setup fee on monthly plans, what month two costs on each plan, and how to test a server properly.
7 min readRead →Hosting Renewal Price vs Intro Price, Explained
Why the hosting renewal price is often 2 to 6 times the intro rate, how to work out the true 3-year cost of web hosting, VPS and email, and what to ask first.
7 min readRead →VPS Hosting Cost in 2026: Prices by RAM Size
How much does VPS hosting cost in 2026? VPS prices for 2 GB to 64 GB of RAM, monthly vs 3-year billing, renewal jumps and hidden costs.
6 min readRead →Ready to deploy?
Pick a plan, choose a term and our team sets up your server for you.
See all plansTalk to salesNeed a custom server, bulk pricing or help moving from another host? Our team will build a quote for you.