serverserv serverserv

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

VariableExample
DB_TYPEpostgresdb
DB_POSTGRESDB_HOSTpostgres
DB_POSTGRESDB_PORT5432
DB_POSTGRESDB_DATABASEn8n
DB_POSTGRESDB_USERn8n
DB_POSTGRESDB_PASSWORDa long random password
DB_POSTGRESDB_SCHEMApublic (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

  1. Export: n8n export:workflow --all --output=workflows.json and n8n export:credentials --all --output=creds.json.
  2. Switch the environment variables to Postgres and restart n8n.
  3. Import: n8n import:workflow --input=workflows.json and n8n import:credentials --input=creds.json. Keep the same encryption key so credentials still decrypt.

Backups and tuning

Prefer not to manage it? Our n8n hosting plans come set up for you, first month $1.

Get n8n hosting

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.

Ready to deploy?

Pick a plan, choose a term and our team sets up your server for you.

See all plansTalk to sales

Need a custom server, bulk pricing or help moving from another host? Our team will build a quote for you.

Plans from $2.99/moSee plans