How to fix FATAL: could not open file 'pg_version': No such file or directory in PostgreSQL

PostgreSQLINTERMEDIATECRITICALUpdated Dec 28, 2025ยท 205 views

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.

Quick fix

Fixes FATAL: could not open file 'pg_version': No such file or directory

grep -i 'data_directory' /etc/postgresql/*/main/postgresql.conf
# or
echo $PGDATA
  1. Verify the data directory path
  2. Check if pg_version file exists
  3. Stop PostgreSQL and backup the data directory
  4. Restore PG_VERSION from a working cluster or create it manually
postgresql โ€” error
grep -i 'data_directory' /etc/postgresql/*/main/postgresql.conf
# or
echo $PGDATA
FATAL: could not open file 'pg_version': No such file or directory

What this error means

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.

Typical symptoms

  • PostgreSQL server fails to start with FATAL error
  • All database connections are rejected
  • Error appears immediately after upgrade attempts
  • Error occurs after data directory backup restoration
  • Docker PostgreSQL containers fail to initialize

Common causes

  1. Data directory was never properly initialized using initdb
  2. Previous pg_upgrade attempt failed and left data directory in incomplete state
  3. pg_version file was accidentally deleted or corrupted
  4. Data directory was copied or restored without preserving all system files
  5. Incorrect data directory path specified in postgresql.conf
  6. File system corruption affected critical PostgreSQL system files
  7. Mixing PostgreSQL versions without proper upgrade procedure

How to fix "FATAL: could not open file 'pg_version': No such file or directory"

1Verify the data directory path

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:

bash
grep -i 'data_directory' /etc/postgresql/*/main/postgresql.conf
# or
echo $PGDATA

The data directory should contain files like PG_VERSION, pg_control, base/, and global/.

2Check if pg_version file exists

List the contents of your data directory to confirm whether pg_version is missing:

bash
ls -la /var/lib/postgresql/data/ | grep -i pg_version

You 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:

bash
ls -la /var/lib/postgresql/data/PG_VERSION

The file should be readable by the postgres user.

3Stop PostgreSQL and backup the data directory

Before making any changes, stop the PostgreSQL server and create a backup:

bash
# 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.backup

This backup is critical - if recovery fails, you'll have the option to restore.

4Restore PG_VERSION from a working cluster or create it manually

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:

bash
cd /var/lib/postgresql/data
sudo cat /path/to/working/data/PG_VERSION

Note the version number (e.g., '13' for PostgreSQL 13). Then create it in your damaged directory:

bash
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_VERSION

Option B: Re-initialize the cluster (data loss)

If you don't have a backup, you must reinitialize the data directory:

bash
# 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/data

This will create a new empty cluster. You'll lose all data unless you have a separate pg_dumpall backup.

5Fix file permissions

After restoring PG_VERSION, ensure correct ownership and permissions:

bash
sudo chown -R postgres:postgres /var/lib/postgresql/data
sudo chmod 700 /var/lib/postgresql/data
sudo chmod 600 /var/lib/postgresql/data/PG_VERSION

On Linux, verify with:

bash
ls -la /var/lib/postgresql/data/ | head -20

The postgres user must be the owner and the data directory must have mode 700 (rwx------).

6Start PostgreSQL and verify

Attempt to start PostgreSQL:

bash
sudo systemctl start postgresql
# or
sudo service postgresql start

Check the status and logs:

bash
sudo systemctl status postgresql
sudo tail -f /var/log/postgresql/postgresql.log

If PostgreSQL starts successfully, verify connectivity:

bash
sudo -u postgres psql -c "SELECT version();"

This command should display the PostgreSQL version information.

Advanced notes

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.

Frequently asked questions

What does "FATAL: could not open file 'pg_version': No such file or directory" mean?
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.
How do I fix "FATAL: could not open file 'pg_version': No such file or directory"?
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.
What causes "FATAL: could not open file 'pg_version': No such file or directory"?
Data directory was never properly initialized using initdb Previous pg_upgrade attempt failed and left data directory in incomplete state pg_version file was accidentally deleted or corrupted
Is the "FATAL: could not open file 'pg_version': No such file or directory" error serious?
Yes. This is a critical-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading