It is time the year to figure out the answer of:

PG::ConnectionBad: 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"?

Check if Postgres started:

ps aux | grep postgres

I‘ve collected all possible answers, try any of these combinations. Good luck!

Solution 1

brew services restart postgresql

Solution 2

This file needs to go away:

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

then brew services restart postgresql.

2019 ✅

Solution 3

Could be there is no default database:

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

2020 ✅

Solution 4

If you recently brew upgrade then maybe you need this:

brew postgresql-upgrade-database

2020 ✅

Solution 5

Restart your computer.