How do I access Postgres when I get an error about “/var/run/postgresql/.s.PGSQL.5432”?

If your Postgres service is up and running without any error or there is no error in starting the Postgres service and still you are getting the mentioned error, follow these steps

Step1: Running pg_lsclusters will list all the postgres clusters running on your device


Ver Cluster Port Status Owner Data directory Log file 9.6 main 5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log 

most probably the status will be down in your case . Try restarting Postgres clusters and service

Step 2: Restart the pg_ctlcluster

#format is pg_ctlcluster <version> <cluster> <action> sudo pg_ctlcluster 9.6 main start #restart postgresql service sudo service postgresql restart 

Step 3: Step 2 failed and threw an error

If this process is not successfull it will throw the error. My error was(You can see the error log on /var/log/postgresql/postgresql-9.6-main.log)

FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied Try adding `postgres` user to the group `ssl-cert` 

Step 4: check ownership of postgres

Make sure that postgres is the owner of /var/lib/postgresql/version_no/main eg: sudo chown postgres -R /var/lib/postgresql/9.6/main/

Step 5: Check Postgres user belongs to ssl-cert user group

It happened to me and it turned out that I removed erroneously the Postgres user from “ssl-cert” group. Run the below code to fix the user group issue and fixing the permissions

#set user to group back with sudo gpasswd -a postgres ssl-cert # Fixed ownership and mode sudo chown root:ssl-cert /etc/ssl/private/ssl-cert-snakeoil.key sudo chmod 740 /etc/ssl/private/ssl-cert-snakeoil.key sudo service postgresql restart 

You probably have multiple PostgreSQL versions installed. If so, the other version probably defaults to unix_socket_directories = '/tmp/' but the libpq your psql is linked to probably defaults to /var/run/postgresql/.


psql -h /tmp 

If that works, the above is the problem. You can add export PGHOST=/tmp to your .bashrc to change the default locally for your user.

If that doesn’t work, make sure PostgreSQL is actually running

ps aux |grep postgres 

and if not, start it. How depends on how you installed it, but it’ll be via the service or systemctl command(s) if you installed using packages.

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”?

This error generally means that the server is not running. Based on dpkg -l output and the thread of comments, it was due to the postgresql-9.5 main package being somehow uninstalled. Since the uninstall hasn’t been called with the --purge option to dpkg, the data and configuration files are still there, so apt-get install postgresql-9.5 can fix the problem.