Updated: 28 September 2026 · Applies to: n8n 2.40 with PostgreSQL 18 (Docker Compose)
By default n8n stores workflows, credentials and execution history in a small built-in SQLite database. That is fine for trying n8n out. For production, meaning several users or workflows that run around the clock, n8n recommends PostgreSQL. This guide adds PostgreSQL to the Docker Compose setup of How to install n8n with Docker Compose and HTTPS on an Ubuntu VPS?.
Important before you start
- n8n does not move existing SQLite data to PostgreSQL by itself. On a new n8n, just follow the steps. On an existing n8n, export your workflows and credentials first and import them afterwards (see the last section).
- Keep the
n8n_datavolume: the.n8nfolder still holds the encryption key and other files when you use PostgreSQL.
1. Add the database settings to .env
POSTGRES_USER=n8n POSTGRES_PASSWORD=CHOOSE-A-LONG-RANDOM-PASSWORD POSTGRES_DB=n8n
Use openssl rand -hex 24 to create the password.
2. Add a PostgreSQL service and volume to compose.yaml
services:
postgres:
image: postgres:18
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
PGDATA: /var/lib/postgresql/data
volumes:
- db-storage:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 10
volumes:
db-storage:
Merge this into your existing services: and volumes: sections; do not create a second services: block. Keep the PGDATA line: PostgreSQL 18 changed its default data folder, and without this line the database starts empty because the volume does not cover the new folder.
3. Point n8n to PostgreSQL
In the n8n service add these lines to environment: and make it wait for the database:
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
- DB_POSTGRESDB_USER=${POSTGRES_USER}
- DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
depends_on:
postgres:
condition: service_healthy
4. Start it
cd ~/n8n-compose sudo docker compose up -d sudo docker compose logs -f n8n
n8n creates its tables in PostgreSQL on the first start. On a new installation, open n8n and create the owner account.
Move an existing SQLite n8n to PostgreSQL
- Make a full backup (How to back up and restore n8n (workflows, credentials and encryption key)?).
- Export workflows and credentials to a folder that is shared with the host (for example
./local-files, seen as/files):sudo docker compose exec n8n n8n export:workflow --backup --output=/files/backup/workflows/ sudo docker compose exec n8n n8n export:credentials --backup --output=/files/backup/credentials/
- Do steps 1 to 4 above. Keep the same
N8N_ENCRYPTION_KEY(and the samen8n_datavolume) so that the exported credentials can be decrypted. - Create the owner account on the new database, then import:
sudo docker compose exec n8n n8n import:workflow --separate --input=/files/backup/workflows/ sudo docker compose exec n8n n8n import:credentials --separate --input=/files/backup/credentials/
- Publish your workflows again. Users, execution history and variables are not part of this export and have to be created again.
Back up PostgreSQL
sudo docker compose exec -T postgres pg_dump -U n8n n8n | gzip > n8n-db-$(date +%Y%m%d).sql.gz
Use your own database user and name if you changed them in .env. Keep this file together with the .n8n volume backup.
Frequently asked questions
Can I upgrade an existing older PostgreSQL to version 18 by changing the image?
No. A major version upgrade of PostgreSQL cannot open the old data folder. Dump the database first (pg_dumpall) and follow the PostgreSQL upgrade guide.
Is the database reachable from the internet?
No. In this setup PostgreSQL has no published port; only the n8n container can reach it.
Official documentation: n8n documentation: Install using Docker Compose (PostgreSQL section) and Back up and restore.
Need a server for n8n, or a hand with the setup?
- n8n VPS hosting: a VPS made for self-hosted n8n.
- n8n setup service: we install n8n on your own server with SSL, backups and security.
- n8n automation services: workflows, integrations and consulting.
Prefer a hand with the setup? Our engineers can do it for you: Hire an Expert, or use our on-demand server management.
