could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket

The issue

We ran brew update on Mac recently. After the update, the postgres count not start and we got the issue. We tried to uninstall postgres and install it again. It didn’t help. We got the following issue when we type:

$ psql
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"?

The solution

A pid file existed as a lock file. The pid file was blocking postgres from starting up. As soon as we remove the pid file the issue was resolved.

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql

Reference

Postgres could not connect to server