Solution 1:

Had a similar problem; a pid file was blocking postgres from starting up. To fix it:

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

and then all is well.


UPDATE:

For Apple M1 (Big Sur) users, do this instead:

$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql

Solution 2:

This can sometimes be an issue with a postgres upgrade.

In my case, it happened when upgrading from 9.3 to 9.4.

See http://www.postgresql.org/docs/9.4/static/upgrading.html

OS X/Homebrew:

Try running postgres -D /usr/local/var/postgres -- it will give you a much more verbose output if postgres fails to start.

In my case, running rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8 removed my old databases and then reinitialized the postgres db schema.

Thanks to https://github.com/Homebrew/homebrew/issues/35240 for that solution.

After regenerating my databases (with rake db:create) everything worked fine again.

Solution 3:

Found a solution that worked for me here:

https://dba.stackexchange.com/questions/75214/psql-could-not-connect-to-server-no-such-file-or-directory

You basically run the following command to manually start the server:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Solution 4:

If installing and uninstalling postgres with brew doesn't work for you, look at the logs of your postgresql installation or:

postgres -D /usr/local/var/postgres

if you see this kind of output:

LOG:  skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.4, which is not compatible with this version 9.6.1.

Then try the following:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

Then start the server:

pg_ctl -D /usr/local/var/postgres -l logfile start

Source