[Fixed] FATAL: database files are incompatible with server in PostgreSQL: Step-by-Step Troubleshooting Guide

Quick Summary: The error FATAL: database files are incompatible with server (followed by DETAIL: The data directory was initialized by PostgreSQL version X, which is not compatible with this version Y) occurs when a PostgreSQL server binary attempts to mount a data directory (PGDATA) created by a different major PostgreSQL release. This most commonly happens in Docker Compose when using an unpinned postgres:latest tag that silently upgrades across major versions, or after a local Homebrew/Linux OS upgrade.

Understanding the Root Causes

PostgreSQL changes its internal binary storage format, system catalog layout, and write-ahead log (WAL) structure between major releases (for example, between PostgreSQL 15, 16, and 17). Unlike minor patch upgrades (such as 16.1 to 16.4), a newer major server binary cannot directly read an older major version’s data directory. The mismatch triggers when:

  • Unpinned Docker Image Tags: Running image: postgres:latest or postgres:alpine in docker-compose.yml and pulling a newer major version while keeping an existing named volume.
  • Homebrew / OS Package Upgrades: Running brew upgrade on macOS or a distribution upgrade on Linux that installs a new PostgreSQL binary pointing at the previous version’s data folder.
  • PostgreSQL 18+ Volume Path Changes: Mounting a host folder directly to /var/lib/postgresql/data when switching between container image generations.

Step 1: Quick Fix – Pin the Matching PostgreSQL Version

First, inspect the PG_VERSION file inside your data directory to confirm which major version initialized the database, then pin your Docker image or local service to that exact major version:

# 1. Check the version recorded in your data directory (local or Docker volume)
cat /var/lib/postgresql/data/PG_VERSION
# Or inspect a Docker named volume using a temporary Alpine container:
docker run --rm -v my_pgdata:/data alpine cat /data/PG_VERSION
# Example output: 15

# 2. Pin docker-compose.yml to the matching major version tag
services:
  db:
    image: postgres:15-alpine  # Pin to match PG_VERSION instead of 'latest'
    volumes:
      - my_pgdata:/var/lib/postgresql/data

# 3. Recreate the container
docker compose up -d

Step 2: Migrate Data Cleanly Using pg_dumpall and Restore

If you intentionally want to upgrade to the newer PostgreSQL major version (e.g., from 15 to 17) in Docker, export your data using the old version and restore it into a fresh volume initialized by the new version:

# 1. Start a temporary container with the OLD version to dump all databases & roles
docker run --rm -v my_pgdata:/var/lib/postgresql/data \
  --name pg-old -d postgres:15-alpine

# Wait a few seconds for startup, then dump everything to a SQL file
docker exec pg-old pg_dumpall -U postgres > full_backup.sql
docker stop pg-old

# 2. Create a fresh volume for the NEW PostgreSQL version and start it
docker volume create my_pgdata_v17
docker run --rm -v my_pgdata_v17:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=your_password \
  --name pg-new -d postgres:17-alpine

# 3. Restore the SQL dump into the new container
cat full_backup.sql | docker exec -i pg-new psql -U postgres

Step 3: In-Place Upgrade on Linux or macOS Using pg_upgrade

For bare-metal Linux servers or macOS Homebrew installations with large databases where dumping and restoring takes too long, use PostgreSQL’s built-in pg_upgrade utility:

# On macOS Homebrew:
brew postgresql-upgrade-database

# On Ubuntu / Debian Linux (using pg_upgradecluster wrapper):
# 1. Stop both clusters and drop the empty default new cluster if initialized
sudo pg_dropcluster 17 main --stop

# 2. Upgrade the older cluster (e.g., 15) in place to 17
sudo pg_upgradecluster 15 main

# 3. Once verified, drop the old 15 cluster to reclaim disk space
sudo pg_dropcluster 15 main

Verification & Testing Steps

Verify that PostgreSQL starts without compatibility errors, reports the expected server version, and contains all migrated tables and data:

# 1. Check container or service logs for clean startup
docker compose logs --tail 30 db

# 2. Confirm server version and readiness
docker exec -it my-postgres-container pg_isready -U postgres
docker exec -it my-postgres-container psql -U postgres -c "SELECT version();"

# 3. List databases and verify row counts
docker exec -it my-postgres-container psql -U postgres -c "\l"

Troubleshooting Summary Comparison

Scenario Primary Root Cause Recommended Solution
Docker stack broke after pull postgres:latest upgraded major version Pin image tag to match PG_VERSION (e.g., postgres:15)
Intentional Docker major upgrade New image cannot read old PGDATA volume Dump with pg_dumpall from old tag, restore into new volume
macOS Homebrew upgrade error brew upgrade bumped PostgreSQL major version Run brew postgresql-upgrade-database
Ubuntu / Debian server upgrade New major package installed alongside old cluster Use sudo pg_upgradecluster <old_ver> main

Leave a Reply