PostgreSQL cannot start because the pg_version file is missing from the data directory. This typically occurs during upgrades, after data directory corruption, or when using an improperly initialized database cluster.
The pg_version file is a critical system file that PostgreSQL uses to identify which version created the data directory. It's located at the root of the data directory and contains only the PostgreSQL version number. When PostgreSQL starts up, it reads this file to ensure the server version is compatible with the data directory. If this file is missing or unreadable, PostgreSQL cannot safely start the database cluster, as it cannot verify compatibility between the server and stored data format.
First, confirm that you're pointing to the correct PostgreSQL data directory. Common paths are /var/lib/postgresql/data (Linux), /Library/PostgreSQL/data (macOS), or C:\Program Files\PostgreSQL\data (Windows).
Check your postgresql.conf file or environment variables:
grep -i 'data_directory' /etc/postgresql/*/main/postgresql.conf
# or
echo $PGDATAThe data directory should contain files like PG_VERSION, pg_control, base/, and global/.
List the contents of your data directory to confirm whether pg_version is missing:
ls -la /var/lib/postgresql/data/ | grep -i pg_versionYou should see a file named PG_VERSION (note the case on Linux/macOS). If it's missing, proceed to the next steps. If it exists but PostgreSQL still reports it's missing, check file permissions:
ls -la /var/lib/postgresql/data/PG_VERSIONThe file should be readable by the postgres user.
Before making any changes, stop the PostgreSQL server and create a backup:
# Stop PostgreSQL
sudo systemctl stop postgresql
# or
sudo service postgresql stop
# Backup the entire data directory
sudo cp -r /var/lib/postgresql/data /var/lib/postgresql/data.backupThis backup is critical - if recovery fails, you'll have the option to restore.
You have two options:
Option A: Restore from a fresh initialization
If you have another PostgreSQL cluster of the same version, copy the PG_VERSION file:
cd /var/lib/postgresql/data
sudo cat /path/to/working/data/PG_VERSIONNote the version number (e.g., '13' for PostgreSQL 13). Then create it in your damaged directory:
sudo bash -c 'echo 13 > /var/lib/postgresql/data/PG_VERSION'
sudo chown postgres:postgres /var/lib/postgresql/data/PG_VERSION
sudo chmod 600 /var/lib/postgresql/data/PG_VERSIONOption B: Re-initialize the cluster (data loss)
If you don't have a backup, you must reinitialize the data directory:
# Remove the corrupted directory
sudo rm -rf /var/lib/postgresql/data
# Reinitialize as postgres user
sudo -u postgres /usr/lib/postgresql/13/bin/initdb -D /var/lib/postgresql/dataThis will create a new empty cluster. You'll lose all data unless you have a separate pg_dumpall backup.
After restoring PG_VERSION, ensure correct ownership and permissions:
sudo chown -R postgres:postgres /var/lib/postgresql/data
sudo chmod 700 /var/lib/postgresql/data
sudo chmod 600 /var/lib/postgresql/data/PG_VERSIONOn Linux, verify with:
ls -la /var/lib/postgresql/data/ | head -20The postgres user must be the owner and the data directory must have mode 700 (rwx------).
Attempt to start PostgreSQL:
sudo systemctl start postgresql
# or
sudo service postgresql startCheck the status and logs:
sudo systemctl status postgresql
sudo tail -f /var/log/postgresql/postgresql.logIf PostgreSQL starts successfully, verify connectivity:
sudo -u postgres psql -c "SELECT version();"This command should display the PostgreSQL version information.
For pg_upgrade failures: If this error occurred during a major version upgrade using pg_upgrade, the upgrade was interrupted. Never manually delete or modify system files during pg_upgrade - if you need to restart, remove the new cluster entirely and run pg_upgrade again. Always run pg_upgrade --check first to verify compatibility.
For Docker users: The pg_version file is created during image build. If you mount a volume, ensure the volume is empty before first start or use a properly initialized volume. The error often occurs when reusing a volume with an old PostgreSQL version.
For tablespace issues: If you see 'pg_tblspc/*/PG_VERSION is missing', the problem is in tablespaces. Run pg_dumpall --schema-only from a working instance to find the tablespace definitions and manually link them correctly.
File system corruption: If you suspect hardware issues, run fsck or equivalent disk check. Kernel logs (dmesg) may show I/O errors. Corrupted file systems can damage the pg_version file beyond repair, requiring full data restoration from backup.
vacuum failsafe triggered
How to fix "vacuum failsafe triggered" in PostgreSQL
PANIC: could not write to file
How to fix PANIC: could not write to file in PostgreSQL
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL