psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory means psql found no server on the local Unix socket it tried. Either point psql at the server you meant with -h host, or start a server on this machine.

When you run psql without -h, it connects through the socket file /var/run/postgresql/.s.PGSQL.5432. In the test that file existed only in the container where the server ran. In the test below the folder was empty, so psql stopped. Adding -h with the server's name made the same command work, and so did starting a server in the same container.

The error

$ docker exec fix8-pg3-client sh -c "psql -U postgres 2>&1"
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

The exit code was 2. pg_isready in the same container answered /var/run/postgresql:5432 - no response, and the socket folder held no files:

$ docker exec fix8-pg3-client sh -c "ls -la /var/run/postgresql/ 2>&1"
total 8
drwxrwsrwt 1 postgres postgres 4096 Aug  5 00:36 .
drwxr-xr-x 1 root     root     4096 Aug  5 00:36 ..

Why it happens

Without -h, this psql picked the Unix socket in /var/run/postgresql, as the message shows. The socket file .s.PGSQL.5432 is created by a running server: in the container where the server ran, the same folder listed .s.PGSQL.5432 and .s.PGSQL.5432.lock. The client container had psql installed but its command was sleep 600, so no server ever started there, while the real server was one container away.

The fix

Way one: the server runs somewhere else, so name its host. Here it is the container fix8-pg3 on a shared Docker network:

$ docker exec -e PGPASSWORD=labpass fix8-pg3-client sh -c "psql -h fix8-pg3 -U postgres -d lab -c 'select version();' 2>&1"
                                                      version
--------------------------------------------------------------------------------------------------------------------
 PostgreSQL 18.4 (Debian 18.4-1.pgdg13+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
(1 row)

Way two: you meant a server on this machine, so start it. In the client container that took a data folder and pg_ctl:

$ docker exec -u postgres fix8-pg3-client sh -c "initdb -D /tmp/pgdata > /tmp/initdb.log 2>&1; pg_ctl -D /tmp/pgdata -l /tmp/pg.log start 2>&1; psql -U postgres -c 'select 1 as ok;' 2>&1"
waiting for server to start.... done
server started
 ok
----
  1
(1 row)

$ docker exec fix8-pg3-client sh -c "pg_isready 2>&1"
/var/run/postgresql:5432 - accepting connections

Use the first way when the server runs in another container or on another host: in the test, psql without -h did not reach it. The second way is for a server you expected in the same container or machine. A third route with the official image is docker exec into the container that runs the server, where psql -U postgres -d lab -c 'select 1 as ok;' answered over the socket, which in that container listed .s.PGSQL.5432.

How it was reproduced

Two containers from the postgres:18 image (psql 18.4, Debian 18.4-1.pgdg13+1) on Docker Engine 29.9.2 under Docker Desktop on Windows 11: fix8-pg3 ran the server normally, fix8-pg3-client was started with sleep 600 as its command, so it had the client tools and no server. Both joined a test network fix8-pg3-net. These container and network names are the test's own.

Frequently asked

What does 'No such file or directory' mean for .s.PGSQL.5432?
psql tried the local Unix socket /var/run/postgresql/.s.PGSQL.5432 and the file was not there. In the test the folder was empty because no server was running in that container.
How do I connect psql to PostgreSQL in another Docker container?
Pass the host with -h. In the test, psql -h fix8-pg3 -U postgres -d lab connected from the client container once both containers were on the same Docker network.
How do I check whether a PostgreSQL server is listening on the socket?
Run pg_isready. It answered '/var/run/postgresql:5432 - no response' before the server started and '/var/run/postgresql:5432 - accepting connections' after pg_ctl start.

More decoded errors in the Fixes category. If psql connects over TCP and is refused instead, see the connection refused post.