PostgreSQL fails to start because the postmaster.pid lock file exists in the data directory, indicating a previous instance did not shut down cleanly. Remove the stale lock file after verifying no PostgreSQL processes are running.
PostgreSQL uses a postmaster.pid lock file in its data directory to track active server instances. This file is created when PostgreSQL starts and removed during normal shutdown. When PostgreSQL encounters an existing lock file, it assumes another instance is already running and refuses to start. This error occurs when a PostgreSQL instance crashes, loses power, or terminates abnormally without removing the lock file. The error manifests as "FATAL: lock file "postmaster.pid" already exists" with a hint about whether another postmaster (and its PID) is running.
First, confirm that PostgreSQL is not running. Check the PID mentioned in the error message.
# Check if the PID from the error is actually running
ps aux | grep <PID_FROM_ERROR>
# Or check all PostgreSQL processes
ps aux | grep postgresIf no PostgreSQL processes appear, proceed to the next step. If a process is running, you must stop it properly before removing the lock file.
Find where PostgreSQL stores its data. The lock file is always located in the root of the data directory as postmaster.pid.
# Common PostgreSQL data directory locations:
# Linux (Debian/Ubuntu)
/var/lib/postgresql/<version>/main/postmaster.pid
# Linux (Red Hat/CentOS/Fedora)
/var/lib/pgsql/data/postmaster.pid
# macOS
/usr/local/var/postgres/postmaster.pid
# Windows
C:\Program Files\PostgreSQL\<version>\data\postmaster.pidVerify the lock file exists:
ls -la /var/lib/postgresql/13/main/postmaster.pidOnce you have confirmed no PostgreSQL process is running, remove the lock file:
# Linux/macOS (use sudo if necessary)
rm /var/lib/postgresql/<version>/main/postmaster.pid
# Or more safely, check permissions first
ls -l /var/lib/postgresql/<version>/main/postmaster.pid
sudo rm /var/lib/postgresql/<version>/main/postmaster.pidImportant: Only remove this file if you are certain no PostgreSQL process is running. Deleting postmaster.pid while PostgreSQL is active can cause severe data corruption.
After removing the stale lock file, restart PostgreSQL:
# Linux with systemd
sudo systemctl restart postgresql
# Linux with service command
sudo service postgresql restart
# macOS with Homebrew
brew services restart postgresql
# Manual startup (if using non-standard installation)
pg_ctl -D /path/to/data/directory startVerify PostgreSQL is running:
# Check service status
sudo systemctl status postgresql
# Or test connection
psql -U postgres -c "SELECT version();"The postmaster.pid file contains exactly eight lines of information including the postmaster process ID, the data directory path, the start time, and shared memory segment information. If the file is empty or corrupted, it indicates an incomplete startup sequence, often due to unclean shutdown. On Windows, pg_ctl may timeout during startup cleanup on the first boot after reboot, leaving an orphaned postgres.exe process and an incomplete lock file. In this case, manually kill the process and remove the lock file. When PostgreSQL is running on multiple machines accessing the same data directory (NFS mount, distributed storage), ensure only one instance can start at a time using filesystem locks or clustering software. If you frequently encounter this error, investigate why PostgreSQL is not shutting down cleanly—this may indicate resource constraints, hung queries, or misconfigured shutdown timeouts.
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
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL