psql cannot connect: no such file or directory on the Unix socket
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directoryShort answer
psql looked for a Unix socket file and found nothing there. Either the server is not running, or it put its socket in a different directory or under a different port than the client expects. Check with pg_isready, then start the server or point psql at the right host and port.
Fix it fast
1Find out which cause you have
Ask the server if it is accepting connections. The exit code separates the cases: 2 means nothing answered.
pg_isready; echo "exit code: $?"2Match what you see, then jump to its fix
- 1
pg_isready exit code 2 with the default socketThe server is not runningFix - 2
The server is up (a postgres process exists) but psql still says the socket is missingThe socket lives in another directory than the client expectsFix - 3
The socket file in the folder is named .s.PGSQL.5433 (or another number), not 5432The server listens on a different portFix
- 1
What does the error look like?
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directoryIs the server running locally and accepting connections on that socket?
Cause 1 of 3
The server is not running
1Check
Read the tail of the server log for the reason it stopped or never started.
ps aux | grep -E '[p]ostgres'
# Debian/Ubuntu packages
pg_lsclusters2Fix
Start the server the way it was installed, then read the log if it exits again. A data directory path in the log tells you which cluster failed.
sudo systemctl start postgresql
# or, for a hand-made cluster
pg_ctl start -D /path/to/data -l logfile3Verify
pg_isready now exits with 0.
pg_isreadyConfirmed by postgresql.org
Cause 2 of 3
The socket lives in another directory than the client expects
1Check
The server's unix_socket_directories decides where the socket file goes; the client's compiled-in default is often /tmp, while packages usually choose another folder.
sudo -u postgres psql -c "SHOW unix_socket_directories"
ls -a /var/run/postgresql /tmp | grep PGSQL2Fix
Pass the directory as the host (a value starting with a slash is a socket directory), or connect over TCP.
psql -h /var/run/postgresql -d postgres
# or
psql -h 127.0.0.1 -d postgres3Verify
The connection opens.
psql -h /var/run/postgresql -d postgres -c "select 1"Confirmed by postgresql.org
Cause 3 of 3
The server listens on a different port
1Check
The socket file name carries the port number, so a client using 5432 misses a server on 5433.
ls -a /var/run/postgresql /tmp | grep PGSQL2Fix
Use that port with -p, or set PGPORT.
psql -p 5433 -d postgres3Verify
pg_isready on the same port exits with 0.
pg_isready -p 5433Confirmed by postgresql.org
How do I stop it coming back?
- Enable the service so it starts at boot (systemctl enable postgresql).
- Set PGHOST and PGPORT in your shell profile when you run several clusters.
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
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
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
manage.py test stops with 'permission denied to create database' on PostgreSQL
django.db.utils.ProgrammingError: permission denied to create database
Django creates a separate database called test_ plus your database name, using the DATABASES USER.
2 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
IntelliJ IDEA Test Connection fails for a PostgreSQL data source
Connection to localhost:5432 refused.
When Test Connection fails, work from the bottom up.
4 causes, each with a fix