PostgreSQL

connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory

connection to server on socket ".s.PGSQL.5432" failed: what it means, why the socket file is missing, and how to fix it or switch to TCP.

What it means

This error comes from psql or another libpq-based client when it tries to reach PostgreSQL through a Unix-domain socket — a file on disk that stands in for a network port — and the file either doesn’t exist or nothing answers behind it. “No such file or directory” means the socket file itself is missing; a close variant, “Connection refused”, means the file is there but nothing is listening on it. Either way, no TCP handshake happens; the client fails before it ever reaches Postgres’s authentication step.

Common causes

  • The server isn’t running. The most common cause by far. If pg_ctl status or your init system reports it stopped, nothing will ever create the socket file.
  • The socket lives in a different directory than the client expects. PostgreSQL’s unix_socket_directories setting controls where the file is created, and it defaults to /tmp on a self-built server but to /var/run/postgresql on Debian/Ubuntu packages. A client with no explicit host, or a hardcoded path from a tutorial written for a different OS, will look in the wrong place.
  • The server only listens on TCP, not on a socket. Setting unix_socket_directories to an empty string means no socket file is ever created — connecting by host/port works, connecting by socket path doesn’t.
  • A stale socket file from a crashed server. If Postgres was killed uncleanly, the old socket file can stay behind with nothing listening on it, and clients get “Connection refused” until the server is started again (a normal start cleans it up).
  • A container or VM where the port is exposed but the socket directory isn’t shared. Socket files never cross the network — they only work for processes on the same filesystem as the server.

How to fix it

  1. Confirm the server is actually up: pg_isready or sudo systemctl status postgresql.
  2. Check where it’s actually listening: sudo -u postgres psql -c "SHOW unix_socket_directories;" (run against a working connection, or check postgresql.conf directly).
  3. Point the client at that same directory, e.g. psql -h /var/run/postgresql -U myuser mydb, or set PGHOST=/var/run/postgresql.
  4. If the server was rebuilt from source, its default socket directory is /tmp — most package-based installs use /var/run/postgresql instead, so check which one you actually have.
  5. If you don’t need a Unix socket at all, connect over TCP instead: psql -h 127.0.0.1 -p 5432 -U myuser mydb. This sidesteps socket paths entirely and is usually the simpler fix for a container, a remote server, or a database reachable only through SSH.
  6. On a fresh install where the server refuses to start at all, check its log for the real startup failure — a missing socket is often a symptom of that, not the root cause.

In Tablyne

Tablyne’s connection form asks for host and port (or an SSH tunnel) rather than a local socket path, so a database that’s only reachable through a socket file on the same machine, or only through SSH, connects the same way: point the form at the tunnel or the TCP port instead of trying to reproduce a socket path.

Ready to stop guessing socket paths? Download Tablyne or open the Postgres GUI.

Sources

Frequently asked questions

Why does the socket path mention /var/run/postgresql on one machine and /tmp on another? +

The path comes from the server's unix_socket_directories setting, not from any client default. Debian and Ubuntu packages default it to /var/run/postgresql; a self-built Postgres defaults to /tmp. A client that assumes the wrong one gets this error even when the server is up.

Is this the same error as 'Connection refused'? +

It's the same family with a different reason. 'No such file or directory' means nothing created the socket file at all (server down, or a different directory). 'Connection refused' means a socket file exists but nothing is listening behind it — usually a server that crashed after creating the socket, or a stale leftover file.

Do I need to fix the socket to connect from another machine? +

No. Unix-domain sockets only work for connections from the same machine. To connect over the network, use a TCP/IP connection string (host + port) instead, and confirm listen_addresses and pg_hba.conf allow it.

Related errors