I was working on one of our Rails apps when it suddenly couldn’t connect to Postgres. I checked Brew services and saw the word “started” colored yellow. This yellow “started” indicates that Brew tried starting Postgres but had issues.
The next step is to open the file
~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist mentioned in the brew services list. Near the bottom of the file, there is a section that points to the Postgres error log file.
The contents of
/usr/local/var/log/postgres.log then pointed to our issue:
FATAL: lock file "postmaster.pid
" already exists HINT: Is another postmaster (PID 532) running in data directory "/usr/local/var/postgres"?
Usually, this points to postgres not being shut down gracefully. A quick
ps aux | grep /usr/local/var/postgresl shows that there aren’t any postgres processes running, confirming a bad shutdown. For me, it was probably the laptop’s battery dying.
The fix is to remove the postmaster.pid file and start postgres. This is acomplished by
rm usr/local/var/postgres/postmaster.pid followed by
brew services stop postgresql && brew services start postgresql.