brew services says PostgreSQL started, but psql cannot connect
brew services list shows postgresql@17 as started, yet psql cannot connectShort answer
brew services start only registers the service with launchd, it does not prove the server is up. Check the status column first. A server that died leaves postmaster.pid behind, and a client may be aimed at a different socket or port than the server uses. Ask pg_isready which of these it is.
Fix it fast
1Find out which cause you have
Ask PostgreSQL itself whether it accepts connections, and look at the service status. Exit code 0 means yes, 2 means no response.
brew services list pg_isready -h localhost -p 54322Match what you see, then jump to its fix
- 1
brew services list does not show the service as started, and pg_isready exits with 2.The service is not runningFix - 2
The service restarts but stays in error; the data directory contains postmaster.pid although nothing runs.A stale postmaster.pid from a crash or a forced shutdownFix - 3
pg_isready works against localhost but psql fails, or the server listens on another port.The client is aimed at another host, port or socket than the serverFix
- 1
What does the error look like?
brew services list shows postgresql@17 as started, yet psql cannot connectpg_isready exits with status 2 (no response to the connection attempt)
Cause 1 of 3
The service is not running
1Check
Homebrew manages the service with launchctl. start registers it to launch at login, run starts it without registering, and a stopped service has to be started again.
brew services info --all2Fix
Restart the service, which stops it if needed and starts it again.
brew services restart postgresql@173Verify
The status shows started, and pg_isready exits with 0.
pg_isready -h localhost -p 5432; echo $?Confirmed by docs.brew.sh
Cause 2 of 3
A stale postmaster.pid from a crash or a forced shutdown
1Check
postmaster.pid is a lock file that records the postmaster's process ID and data directory, and it is not present after a clean shutdown. A copy left behind after a crash can stop a new start. Be sure no postgres process is alive before touching it.
ps aux | grep '[p]ostgres'
ls <data-directory>/postmaster.pid2Fix
If no postgres process is alive, remove the stale file and start the service again.
rm <data-directory>/postmaster.pid
brew services start postgresql@173Verify
pg_isready exits with 0.
pg_isready -h localhost -p 5432Confirmed by postgresql.org
Cause 3 of 3
The client is aimed at another host, port or socket than the server
1Check
Without a host, psql uses a Unix-domain socket in the default socket directory; with a port in PGPORT or the command line, it uses that port.
echo "$PGHOST $PGPORT"
which psql2Fix
Use the same host and port as the server, for example over TCP, or unset stray variables.
psql -h localhost -p 5432 postgres3Verify
psql opens a session.
psql -h localhost -p 5432 postgres -c 'SELECT 1;'Confirmed by postgresql.org
How do I stop it coming back?
- Keep one PostgreSQL major version installed through Homebrew, or note the port each one uses.
- Use brew services restart after an upgrade so launchd starts the new version.
- Do not delete files inside the data directory unless you know what each one does.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
Postgres.app on macOS will not start a server on port 5432
Could not delete stale postmaster.pid file
Postgres.app fails to start when something else already holds port 5432, when another server uses the same data directory, or when a crash left a stale postmaster.pid.
4 causes, each with a fixDatabases
Cannot connect to PostgreSQL on port 5432 from another machine
FATAL: no pg_hba.conf entry for host "198.51.100.7", user "app", database "appdb"
Two settings guard remote access.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
DBeaver cannot download the PostgreSQL driver from Maven
The PostgreSQL driver files are missing and the download does not complete
DBeaver downloads the PostgreSQL JDBC driver from a Maven repository the first time you connect.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fix