Sometimes when you mac shuts down unexpectedly while running a PostgreSQL database it can result in the following error:
Stale postmaster.pid file.
FATAL: lock file “postmaster.pid” already exists
The following tutorial will show you how to delete the postmaster.pid file so that you can restart your PostgreSQL service and run it as intended.
1: Open your terminal
You can find your terminal your applications folder, or use the launcher by clicking cmd + spacebar and search for "terminal".
2: Locate your Postgres directory
How you locate the Postgres directory and pid file will vary from situation to situation as it depends on how you installed PostgreSQL in the first place.
But most commonly your Postgress installation will be located here:
cd Library/Application\ Support/Postgres
3: Locate the postmaster.pid file
Make sure that you are in the right directory by typing
ls in your terminal. This will list out all the files and directories of your current location.
If you are in the right place the terminal should show a folder name
var-10 if you are running PostgreSQL 10. If you running PostgreSQL 11 the folder will be named
var-11, you get the point.
Enter the folder with a
Run an ls to list the folder contents.
var-10 ls PG_VERSION pg_notify pg_wal base pg_replslot pg_xact global pg_serial postgresql.auto.conf pg_commit_ts pg_snapshots postgresql.conf pg_dynshmem pg_stat postgresql.log pg_hba.conf pg_stat_tmp postmaster.opts pg_ident.conf pg_subtrans postmaster.pid pg_logical pg_tblspc pg_multixact pg_twophase
As you can see among the files you find the postmaster.pid, perfect.
4: Remove the postmaster.pid file
Go ahead and remove the postmaster.pid file by running a rm "filename"
When you have removed the stale postmaster.pid file you can restart PostgreSQL and everything should work as normal.