debian postgresql : error: connection to server on socket…
- User
debian postgresql : error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
- Assistant
That error almost always means “there’s no server listening where
psql(or another PostgreSQL client) thinks it should be.”
On Debian/Ubuntu systems the server normally writes its Unix‑domain socket into/var/run/postgresql/(a tmpfs that is recreated on every boot) and the default port is 5432.
Below is a quick checklist to get your instance back online.────────────────────────────────────────────────────────────────────
- Is PostgreSQL installed?
$ dpkg -l | grep postgresqlIf you see nothing, install it:
sudo apt update sudo apt install postgresql──────────────────────────────────────────────────────────────────── 2. Is any cluster defined? (Debian uses the “postgresql‑common” framework)
$ pg_lsclustersTypical healthy output:
Ver Cluster Port Status Owner Data directory Log file 16 main 5432 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log• Nothing listed? → create a cluster:
sudo pg_createcluster 16 main # replace 16 with the version you have sudo systemctl start postgresql──────────────────────────────────────────────────────────────────── 3. Is the service running?
$ sudo systemctl status postgresqlIf it’s inactive or failed:
sudo systemctl start postgresql # start it now sudo systemctl enable postgresql # start automatically at bootStill fails? Look at the logs:
sudo journalctl -u postgresql --since -1h # last hour # or cat /var/log/postgresql/postgresql-*-main.logCommon things you might see: • “could not open file …/pg_hba.conf” → permissions or missing files
• “FATAL: data directory … has wrong ownership” → fix withchown -R postgres:postgres /var/lib/postgresql/<ver>/main──────────────────────────────────────────────────────────────────── 4. Did the socket directory get moved?
postgresql.confparameterunix_socket_directoriescontrols where the server puts the socket.
Check:sudo -u postgres grep unix_socket /etc/postgresql/*/main/postgresql.confIf someone changed it, either • revert it to
/var/run/postgresql, or
• tell the client where to look:psql -h /custom/socket/dir──────────────────────────────────────────────────────────────────── 5. Is the client using the right port?
If
postgresql.confwas changed to a non‑standard port (say 5433):psql -p 5433──────────────────────────────────────────────────────────────────── 6. Are you connecting as the right user?
On Debian it’s easiest to test as the dedicated postgres account:
sudo -iu postgres psqlIf that works but your normal user cannot, the issue is
pg_hba.conf.──────────────────────────────────────────────────────────────────── 7. Nothing works, but processes exist?
ps aux | grep '[p]ostgres'If you see processes but still get the socket error, it usually means: • The server started with a custom
unix_socket_directories(see step 4), or
• You’re mixing versions (e.g.,psql 15tries to talk to server 16).Check the client version:
psql --version…and compare with the server version from
pg_lsclusters.──────────────────────────────────────────────────────────────────── Quick‑fix summary ───────────────── sudo apt install postgresql # if it isn’t installed
sudo systemctl start postgresql # (re)start the service
pg_lsclusters # confirm cluster status
sudo tail -f /var/log/postgresql/... # watch the log for errorsOnce you see the cluster status as “online” and the log shows “database system is ready to accept connections,” your
psqlcommand should succeed.Hope this gets you connected! If you still see the same error after going through these steps, drop me the output of
pg_lsclustersand the last 20 lines of your PostgreSQL log and we’ll dig deeper.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.