Thoughts about tech, programming, Bitcoin, and more.

Fix for error on M1 Mac: connection to server at "localhost" (::1), port 5432 failed: Connection refused

I restarted my MacBook tonight and when I attempted to start up my Rails development environment I encountered the following error:

connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

The problem is due to PostgreSQL not properly closing before the machine was restarted and then a straggling postmaster.pid file is left, making the machine think that the port is in use.

Remove the file on an M1 Mac (or M2... any Apple Silicon)  with the following command:

rm /opt/homebrew/var/postgres/postmaster.pid

Then restart PostgreSQL (this only works if PostgreSQL was installed using Homebrew).

brew services restart postgresql

By now this has happened enough times after restarting my Mac that I'm confident that I can remove the file, but if you're unsure it's always good to double check /opt/homebrew/var/log/postgres.log.

Subscribe to Daniel Lemky

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe