How to Upgrade n8n with PostgreSQL 18 Using Docker Compose and Prevent Data Loss
The n8n project has published a Docker Compose configuration that runs the workflow automation platform with PostgreSQL 18, warning that the database upgrade is “breaking” and requires a manual dump-and-restore process to avoid data loss.
The example, maintained in the n8n-hosting repository, pins the PostgreSQL data directory to /var/lib/postgresql/data because Postgres 18 changed its default storage location, and the volume mount would otherwise become unstable across major versions, according to Githubusercontent.
The compose file defines three services: postgres using the postgres:18 image, n8n referencing docker.n8n.io/n8nio/n8n:${N8N_VERSION}, and an n8n-runner on the same version tag.
Environment variables for database credentials, the non-root user, and runner authentication are sourced from a .env file that the project says must be edited before first start to change default users and passwords. The n8n service depends on a healthy postgres container and mounts a dedicated n8n_storage volume at /home/node/.n8n to hold the encryption key.
Upgrading an existing deployment from PostgreSQL 16 to 18 cannot be done by changing the image tag alone. “Postgres 18 cannot read a data directory written by 16, so bumping the image alone stops with: FATAL: database files are incompatible with server,” the documentation says.
Nothing is deleted, and the image can be pinned back to postgres:16 at any point. For a small setup, the project recommends the official PostgreSQL dump-and-restore guide, executed while n8n is offline.
The published procedure loads the .env file into the shell, exports a full cluster dump with pg_dumpall, verifies the dump contains the completion marker, stops the stack, removes only the withpostgres_db_storage volume, starts the database container alone, imports the dump, then brings up the remaining services.
The instructions explicitly warn against using docker compose down -v, which would also delete n8n_storage and render saved credentials unreadable. Errors such as “role “…” already exists” and “database “…” already exists” on import are expected because the container creates both on first start; the data still imports.
The N8N_VERSION variable in .env pins the n8n release the example is tested with and will move to the 3.x line when 3.0 becomes stable.
Before upgrading to n8n 3.0, users are directed to read the breaking-changes documentation and open the Migration Report in Settings. “n8n migrates its database forward when it starts, so the backup is your way back to 2.x,” the guide says. Staying on PostgreSQL 16 remains supported by pinning the image back to postgres:16.