> ## Content Index
> Fetch the complete content index at: https://www.ggorantala.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# PostgreSQL Socket '.s.PGSQL.5432': No Such File or Directory
- URL: https://www.ggorantala.dev/postgresql-unix-socket-no-such-file/
- Published: 2026-11-30T17:33:03.000Z
- Updated: 2026-11-30T17:33:03.000Z
- Description: '.s.PGSQL.5432: No such file or directory' means psql looked for a Unix socket in one directory and found nothing. The server is stopped, on another port, or puts its socket somewhere else. Here's how to tell which, and fix it.
- Author: Gopi Gorantala
- Tags: unix-socket, unix-socket-directories, psql, libpq, pg-connections, postgresql, postgresql-errors

The most useful thing to know about `.s.PGSQL.5432: No such file or directory` is that it's a file-not-found error. That's all. Your client went to a directory, looked for a file with a particular name, and the file wasn't there. Nothing went over the network and no PostgreSQL code ran. I'll admit the irony: the sandbox I wrote this in had no Docker daemon running, and Docker's own complaint was `dial unix /var/run/docker.sock: connect: no such file or directory`. Same class of problem, different program.

## The exact message, old and new

From `psql` 16.15 on Ubuntu 24.04 (libpq 16.15, the distro package), with a perfectly healthy PostgreSQL 16.15 server running on the same machine:

```log
$ psql -U postgres -d postgres -c 'select 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?

```

That's the wording from libpq 14 onwards. If your client links an older libpq (13 or earlier), you'll see the version people have been pasting into the pgsql-general list for a decade. I didn't run a 13 client for this, so this one is the wording as it appears in those threads, not lab output:

```log
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

```

Two other endings share the same first half, and they mean different things. You'll meet both further down:

```log
connection to server on socket "/home/pglab/sock/.s.PGSQL.5432" failed: Permission denied
connection to server on socket "/var/run/postgresql/.s.PGSQL.5433" failed: Connection refused

```

No `FATAL:`, no SQLSTATE. The server never heard from you, so there's nothing for [reading the error fields](https://ggorantala.dev/read-postgresql-error-message/?ref=ggorantala.dev) to work with. Everything you need is in that quoted path.

## Is there a socket file, and where?

A Unix-domain socket is a special file that two processes on the same machine use to talk, without TCP. When you don't give libpq a host (no `-h`, no `host=`, no `PGHOST`), it doesn't go to `localhost`. It goes to a socket file at `<directory>/.s.PGSQL.<port>`, and the directory is whatever was compiled into that particular libpq.

So read the path in the error as two facts: the directory your client looked in, and the port it assumed (the port is part of the file name). Then check whether the server agrees:

```bash
ls -la /var/run/postgresql/ /tmp/ 2>/dev/null | grep PGSQL   # where are the socket files right now?
pg_isready; echo $?                                          # 2 = nothing at the default socket
env | grep '^PG'                                             # PGHOST / PGPORT override everything
pg_config --configure | tr ' ' '\n' | grep -i sock           # sometimes shows a custom socket dir

```

If you can connect any other way (`-h localhost`, or as the server's OS user), ask the server where it put the socket:

```sql
SHOW unix_socket_directories;
SHOW port;

```

On my lab box, `pg_isready` with no arguments said `/var/run/postgresql:5432 - no response` and exited 2, while `pg_isready -h /tmp` said `accepting connections`. That's the whole diagnosis in two commands. The server was fine. It was just listening in `/tmp`, and Ubuntu's libpq doesn't look there.

## Four ways to lose a socket file

All of this ran on Ubuntu 24.04 with the distro's PostgreSQL 16.15 binaries, as an unprivileged user, in a scratch data directory. Nothing shared, nothing in production. I couldn't run the Docker variant (no daemon, as mentioned), so that one is described, not shown.

**Setup.** A throwaway cluster:

```bash
B=/usr/lib/postgresql/16/bin; export PATH=$B:$PATH
initdb -D ~/pglab -U postgres --auth=trust >/dev/null    # trust is fine for a lab nobody else can reach; never elsewhere

```

**1\. Server puts its socket in `/tmp`, client looks in `/var/run/postgresql`.** This is the Homebrew-meets-Ubuntu situation, and the one I see most on developer laptops with two installs.

```log
$ pg_ctl -D ~/pglab -l ~/pglab.log -o "-c unix_socket_directories=/tmp -c lc_messages=C" -w start
$ grep listening ~/pglab.log
LOG:  listening on IPv4 address "127.0.0.1", port 5432
LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
$ psql -U postgres -d postgres -c 'select 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?
$ psql -h /tmp -U postgres -d postgres -Atc 'show unix_socket_directories'
/tmp

```

The failure came back in 13 ms. A host that starts with `/` is a directory, not a host name, which is why `-h /tmp` works.

**2\. Right directory, wrong port.** Restarted with `port=5433` and the socket in `/var/run/postgresql`:

```log
$ ls -a /var/run/postgresql/
.  ..  .s.PGSQL.5433  .s.PGSQL.5433.lock
$ psql -U postgres -d postgres -c 'select 1'
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory

```

The file is right there. It's called 5433\. `psql -p 5433` connects.

**3\. A leftover `PGHOST`.** Same server, but a shell with `PGHOST=/tmp` exported from some other project:

```log
$ PGHOST=/tmp PGPORT=5433 psql -U postgres -d postgres -c 'select 1'
psql: error: connection to server on socket "/tmp/.s.PGSQL.5433" failed: No such file or directory

```

**4\. The server isn't running.** Stop it with `pg_ctl stop` and the postmaster deletes its socket file on the way out, so you get the same "No such file" as cases 1 to 3\. Which is exactly why this error is so annoying: four causes, one message.

And the two other endings. A server that dies without cleaning up (I sent `kill -9` to the postmaster; a power cut does the same) leaves the socket file behind, and connecting to a socket file with nothing behind it gives you `Connection refused` instead. And a socket directory your user can't traverse (mine was `~/sock`, mode 700, tried from a second user) gives `Permission denied`.

Teardown: `pg_ctl -D ~/pglab -m fast stop`, then `rm -rf ~/pglab ~/pglab.log`. If you killed the server with `-9`, also remove the stale `.s.PGSQL.*` and `.s.PGSQL.*.lock` files, and make sure no orphaned `postgres` child processes are left, or the next start fails with `lock file "postmaster.pid" already exists`. Mine did.

**Docker (not run here).** The official `postgres` image keeps its socket inside the container's filesystem. A host `psql` with no `-h` looks in the host's `/var/run/postgresql` and finds nothing, even with the container running and port 5432 published. Use `psql -h localhost -p 5432` from the host, or `docker exec -it <container> psql -U postgres` to use the socket from inside.

## The one-paragraph version

Your client and your server disagree about where the socket file lives or what it's called, or the server isn't running. The client's directory is compiled into libpq, the server's comes from `unix_socket_directories`, and the port is part of the file name. Point the client at the right directory and port, or start the server.

## Where libpq thinks the socket lives

Here's the part that makes this confusing on machines with more than one PostgreSQL. Upstream PostgreSQL compiles in `/tmp` as the default socket directory for both the server and libpq (it's in the stock `postgresql.conf.sample`: `#unix_socket_directories = '/tmp'`). Packagers change it. Debian and Ubuntu patch both sides to `/var/run/postgresql`; you can see it in the Ubuntu library itself, `strings /usr/lib/x86_64-linux-gnu/libpq.so.5` shows `/var/run/postgresql`, and the packaged `/etc/postgresql/16/main/postgresql.conf` sets `unix_socket_directories = '/var/run/postgresql'`. Fedora and RHEL carry a `var-run-socket` patch that does the same for libpq, while their server listens in both places. Homebrew and Postgres.app on macOS stay with upstream's `/tmp`.

Each pair is consistent with itself. The trouble starts when you mix them: Homebrew's server with a `psql` from somewhere else on your `PATH`, a PGDG server with an older client, a Python wheel that bundles its own libpq (psycopg2-binary and `psycopg[binary]` both do), or a hand-started server with a distro client. The client's library decides where it looks, not the server you meant. When I hit this the first time, my instinct was that the server was down, because "is the server running locally" is what the hint asks. I restarted it. Twice. The log said `listening on Unix socket "/tmp/.s.PGSQL.5432"` both times, and I only read it after the second restart.

Then the thing that surprised me while building the lab. `unix_socket_directories` takes a comma-separated list, and I assumed a bad entry would be skipped. It isn't, at least not when the directory is missing. With `'/var/run/pg-missing,/tmp'` the server refused to start at all:

```log
LOG:  listening on IPv4 address "127.0.0.1", port 5432
FATAL:  could not create lock file "/var/run/pg-missing/.s.PGSQL.5432.lock": No such file or directory
LOG:  database system is shut down

```

That matters after a reboot. `/var/run` is a tmpfs on systemd machines, so `/var/run/postgresql` disappears at every boot and packages recreate it with a `tmpfiles.d` rule. Start PostgreSQL by hand, or from a custom unit, and nothing recreates it. Same with permissions: my unprivileged lab user wasn't in the `postgres` group, couldn't write into `/var/run/postgresql`, and the server died with `could not create lock file "/var/run/postgresql/.s.PGSQL.5433.lock": Permission denied`. The client then reports "No such file or directory", which is true and points nowhere useful. The [server log](https://ggorantala.dev/postgresql-server-logs-location/?ref=ggorantala.dev) is where the real reason is.

One more trap with no clear error at all: systemd's `PrivateTmp=true`. A service started with it (Apache and PHP-FPM units are the classic examples on the mailing lists) gets its own private `/tmp`. If your server's socket is in `/tmp`, that service can't see it, while `psql` from your shell connects fine. It's another argument for not keeping sockets in `/tmp`.

Clients that don't use libpq play by their own rules. pgJDBC never uses a Unix socket unless you configure a socket factory such as junixsocket, so a Java app on the same box goes over TCP and can work while `psql` fails. node-postgres defaults `host` to `localhost` (TCP) and uses a socket only when the host starts with `/`. On Windows, libpq defaults to TCP on `localhost`, so you'll see [connection refused](https://ggorantala.dev/postgresql-connection-refused-port-5432/?ref=ggorantala.dev) there rather than this.

## Fixing 'No such file or directory' on the socket

Decide which case you're in from the path in the error, then change one thing.

| What you found                                                   | Fix                                                                                                                 |
| ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| Socket exists in another directory (/tmp vs /var/run/postgresql) | Connect with \-h /tmp or set PGHOST=/tmp for that environment, or align the server (below)                          |
| Socket file has a different port number                          | Use \-p 5433 or PGPORT; don't renumber the server to suit a client                                                  |
| PGHOST/PGPORT exported from another project                      | Unset them, or move them into a per-project service file                                                            |
| No socket file anywhere, server not running                      | Start it, then read the server log for why it stopped                                                               |
| Server won't start: could not create lock file                   | Create the directory with the right owner, or fix the packaging rule that should create it                          |
| Connection refused on the socket                                 | Stale file from a crash; confirm no postmaster is running, remove the stale socket and lock files, start the server |
| Permission denied on the socket                                  | Your OS user can't traverse the directory; fix group membership or directory mode, don't chmod 777 it               |

If you control the server and want it to serve both kinds of client, list both directories. It's a `postmaster`\-context setting (restart required; upstream default `/tmp`, Debian/Ubuntu packages set `/var/run/postgresql`):

```ini
# postgresql.conf, PostgreSQL 16; restart, not reload
unix_socket_directories = '/var/run/postgresql, /tmp'

```

Both directories must exist and be writable by the server's OS user before the restart, or it won't come up, as shown above.

Reaching for `-h localhost` is a fine workaround, with one catch. It switches you from a `local` line to a `host` line in `pg_hba.conf`, so authentication may change under you. That's how people trade this error for [Peer authentication failed](https://ggorantala.dev/postgresql-peer-authentication-failed/?ref=ggorantala.dev) or a password prompt they didn't expect. On my lab cluster `\conninfo` said `via socket in "/var/run/postgresql"` for one and `on host "127.0.0.1"` for the other, matched by different lines in `pg_hba_file_rules`. Know which one you want.

What isn't a fix: symlinking `/tmp/.s.PGSQL.5432` into `/var/run/postgresql` by hand. It works until the next restart or reboot, then you're back here with a dangling link.

## One socket directory, everywhere

On a laptop, run one PostgreSQL install per major version and use its own client tools. If Homebrew owns the server, make sure `which psql` points at Homebrew's too. Mixed installs are where this error lives.

For anything scripted, stop relying on compiled-in defaults. Spell out the socket directory or host every time:

```ini
# ~/.pg_service.conf
[invoices-local]
host=/var/run/postgresql
port=5432
dbname=invoice_archive
user=invoice_app

```

Then `psql service=invoices-local`, and your invoice archive's scripts behave the same whichever libpq happens to load them. The [connection string formats](https://ggorantala.dev/postgresql-connection-string-formats/?ref=ggorantala.dev) article covers the URI form of the same thing (`postgresql://invoice_app@%2Fvar%2Frun%2Fpostgresql/invoice_archive`, with the slashes percent-encoded).

On servers, keep the socket out of `/tmp` and let the package's `tmpfiles.d` rule own the directory.

## Keeping it from coming back

Use `pg_isready -h /var/run/postgresql -p 5432` (explicit directory and port) in health checks and deploy scripts, never the bare command, which inherits whatever the local libpq was built with. Exit code 2 means nothing at that socket.

After any upgrade or reinstall, check `grep 'listening on Unix socket' <server log>` against the path your client prints. When they differ, you've found the problem before anyone else does.

Version notes: libpq 14 changed the wording to `connection to server on socket "…" failed:`; 13 and older say `could not connect to server: No such file or directory`. Multiple socket directories have been allowed since 9.3, so every supported release (15 through 19, as of November 2026) takes a list. The default directory depends on who built your binaries, not on the PostgreSQL version.

What I checked for this one: the 16.15 server and client on Ubuntu 24.04 for every variant above, the compiled-in directory in Ubuntu's libpq, the packaged config, and the RHEL socket patch. The old wording comes from mailing-list reports, not my lab. If you're on a different packager, `strings` on your libpq will tell you its default in a second.

## If you remember nothing else

- The path in the error is where the client looked. Compare it with `listening on Unix socket` in the server log.
- The port is part of the file name. `.s.PGSQL.5433` won't answer to a client expecting 5432.
- `-h /tmp` means "socket in `/tmp`", not a host. `-h localhost` means TCP, and different `pg_hba.conf` rules.
- `Connection refused` on a socket is a stale file from a crash; `Permission denied` is directory access.
- A missing directory in `unix_socket_directories` stops the server from starting.

## What people search for next

**How do I fix '.s.PGSQL.5432 No such file or directory' on macOS with Homebrew?** Homebrew's PostgreSQL uses `/tmp`, so the error usually means the server isn't running (`brew services list`) or a non-Homebrew `psql` is first on your `PATH` and looking in `/var/run/postgresql`. Check `which psql`, then try `psql -h /tmp`. If it's a fresh install, you may hit [role does not exist](https://ggorantala.dev/postgresql-role-does-not-exist/?ref=ggorantala.dev) next.

**Why does psql work as the postgres user but not in my app?** Your app's driver might use a different libpq (a bundled one in a Python wheel, say) with a different compiled-in socket directory, or it might run under systemd with `PrivateTmp=true` and not see `/tmp`. Give the app an explicit host: a socket directory path or `localhost`.

**Should I use -h localhost instead of the socket?** It works, but it goes over TCP and matches `host` lines in `pg_hba.conf` instead of `local` lines, so the authentication method can change. Sockets are a bit faster and allow peer authentication. Pick one deliberately and put it in the connection string.

**I get this error with PostgreSQL in Docker. Why?** The socket file is inside the container, so a host `psql` without `-h` won't find it. Connect over the published port with `psql -h localhost -p 5432`, or run `psql` inside the container with `docker exec`. If TCP then fails too, see [connection refused vs pg\_hba vs password failed](https://ggorantala.dev/postgresql-connection-errors-compared/?ref=ggorantala.dev).