Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

psql cannot connect: no such file or directory on the Unix socket

Errorpsql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory

Short 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.

  • Applies to PostgreSQL 16, PostgreSQL 17, Ubuntu 24.04, macOS 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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: $?"
  2. 2Match what you see, then jump to its fix

    1. 1pg_isready exit code 2 with the default socketThe server is not runningFix
    2. 2The 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. 3The socket file in the folder is named .s.PGSQL.5433 (or another number), not 5432The server listens on a different portFix

What does the error look like?

  • psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
  • Is 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_lsclusters

2Fix

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 logfile

3Verify

pg_isready now exits with 0.

pg_isready

Confirmed by postgresql.org

Did this fix it?

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 PGSQL

2Fix

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 postgres

3Verify

The connection opens.

psql -h /var/run/postgresql -d postgres -c "select 1"

Confirmed by postgresql.org

Did this fix it?

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 PGSQL

2Fix

Use that port with -p, or set PGPORT.

psql -p 5433 -d postgres

3Verify

pg_isready on the same port exits with 0.

pg_isready -p 5433

Confirmed by postgresql.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.