Fix psql 'could not connect to server' in PostgreSQL
By Flavio Copes
Diagnose the psql could not connect to server error on macOS by checking the Homebrew service, logs, and connection layers one at a time.
I had this problem: I had installed PostgreSQL in the past using Homebrew, then I think I updated its version mindlessly with brew upgrade, and I couldn’t start it again.
It used to work, but now whenever I tried to access it, I got this error:
psql: error: 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 server was running fine, as ps aux | grep postgres showed its process.
Back then I fixed it by reinstalling PostgreSQL. That worked, but I would not recommend it as a first move today: a reinstall can create a fresh, empty data directory, which is a polite way of saying it can disconnect you from your existing databases. Diagnose first.
Read the error before touching anything
Connection failures happen at one of a few layers, and each layer produces a different message.
A missing socket, like the error above, means the client found no local server at the expected socket path. Either the service is down, or the server is listening on a different socket directory than the client checks — which is exactly what happens after a Homebrew upgrade moves you to a new major version whose server refuses the old data directory.
“Connection refused” means nothing is listening on that host and port: wrong port, wrong host, or a stopped server.
A FATAL message like “password authentication failed” or “role does not exist” is actually progress. The server answered; you are just not getting in. Check the role, password, database name, and pg_hba.conf.
Check the service and its logs
On macOS with Homebrew:
brew services list
tail -50 /opt/homebrew/var/log/postgresql@18.log
Adjust the log filename to your formula version. If the service shows error, the log almost always names the cause. The line to look for after an accidental major upgrade is:
FATAL: database files are incompatible with server
That means the new server binaries found data files from an older major version. The fix is a proper upgrade with pg_upgrade (or a dump from the old version and restore into the new one), not deletion. Homebrew’s versioned formulas, like postgresql@18, exist so brew upgrade cannot silently jump major versions again.
Then test the connection explicitly, removing every default from the equation:
psql -h localhost -p 5432 -U youruser -d postgres
Change one thing at a time and retry the same command. If reinstalling is genuinely the right call — say, a broken half-installed formula and no data worth keeping — back up the data directory first, then run:
brew uninstall postgresql
brew install postgresql
brew services start postgresql
That is the sequence that got me back to a working database, and with an empty cluster it is harmless. With data you care about, treat it as the last resort, not the first.
Related posts about database: