What to do when postgres suddenly will not start

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the databases category.

Last Updated: 2022-12-01

Case 1: Bad shutdown

Typing psql after a bad shutdown gives the following error:

psql: could not connect to server: No such file or directory

Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Often the problem is a .pid file hanging around. This can be resolved as follows:

rm /usr/local/var/postgres/postmaster.pid

Case 2: Dependency issues

The workflow begins by inspecting logs. On Mac ~/Library/Logs contains logs for applications I set up.

Entries related to postgres can be found with:

$ find . -name *postgres*

In my case, this shows a ton of files indicative of crashes:

./DiagnosticReports/postgres_2019-08-20-195715_Jacks-MacBook-Pro.crash
./DiagnosticReports/postgres_2019-08-20-200706_Jacks-MacBook-Pro.crash
./DiagnosticReports/postgres_2019-08-20-205835_Jacks-MacBook-Pro.crash
./DiagnosticReports/postgres_2019-08-21-125702_Jacks-MacBook-Pro.crash
...

Let's view the most recent one:

find . -name *postgres* | sort | tail -1 | xargs cat

Bingo, here's the problem - a dependency went missing (probably due to some homebrew install)

Termination Reason:    DYLD, [0x1] Library missing

Application Specific Information:
dyld: launch, loading dependent libraries

Dyld Error Message:
  Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.63.dylib
  Referenced from: /usr/local/opt/postgresql/bin/postgres
  Reason: image not found