The error message you're seeing:
connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
means that the PostgreSQL client (like psql) is trying to connect to the PostgreSQL server via a Unix socket, but the socket file does not exist. This usually indicates that the PostgreSQL server is not running, or it's configured to use a different socket directory.
Here’s how to troubleshoot and fix this issue on a Debian-based system:
✅ 1. Check if PostgreSQL is running
Run:
sudo systemctl status postgresql
If it's not running, start it:
sudo systemctl start postgresql
To enable it to start on boot:
sudo systemctl enable postgresql
✅ 2. Check the socket directory
By default, PostgreSQL uses /var/run/postgresql for its Unix socket. If the server is running but the socket is not there, it might be using a different directory.
Check the actual socket directory by running:
sudo -u postgres psql -c "show unix_socket_directories;"
If you can't connect via psql, check the PostgreSQL configuration file:
sudo grep unix_socket_directories /etc/postgresql/*/main/postgresql.conf
Make sure it is set to:
unix_socket_directories = '/var/run/postgresql'
If it's different, either change it to the default or connect using the correct socket path.
✅ 3. Check PostgreSQL logs
If the server fails to start, check the logs for errors:
sudo journalctl -u postgresql
Or check the log files directly, usually in:
/var/log/postgresql/
✅ 4. Reinstall or reinitialize PostgreSQL (if needed)
If PostgreSQL is not installed or the data directory is missing/corrupted, you may need to reinstall or reinitialize it:
sudo apt update
sudo apt install postgresql
✅ 5. Connect using TCP/IP (as a workaround)
If the Unix socket is not working, you can try connecting via TCP/IP:
psql -h 127.0.0.1 -U postgres
Make sure PostgreSQL is configured to listen on localhost. Check:
sudo grep listen_addresses /etc/postgresql/*/main/postgresql.conf
It should be:
listen_addresses = 'localhost'
Then restart PostgreSQL:
sudo systemctl restart postgresql
Summary
- Ensure PostgreSQL is running.
- Verify the socket directory.
- Check logs for errors.
- Use TCP/IP as a fallback.
Let me know if you need help with any of these steps!