How do I access Postgres when I get an error about "/var/run/postgresql/.s.PGSQL.5432"? I am running Ubuntu 16.  I have installed Postgresql.  Postgresql used to work, but then I rebooted.  

nmap commands show port 5432 is open.  Postgres seems to be working correctly:

service postgresql status



  postgresql.service - PostgreSQL RDBMS    Loaded: loaded
  (/lib/systemd/system/postgresql.service; enabled; vendor preset:
  enabled)    Active: active (exited) since Sat 2017-07-29 18:42:59 EDT;
  1min 4s ago   Process: 201 ExecStart=/bin/true (code=exited,
  status=0/SUCCESS)  Main PID: 201 (code=exited, status=0/SUCCESS)
  Memory: 0B    CGroup: /system.slice/postgresql.service


I ran this: psql

But I got this:


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


The file listed above does not seem to exist.

How do I get into Postgresql?  Normally I'd run psql or sudo -i -u postgres then psql.  But these commands are not working.  I keep getting an error about "could not connect to server."  Several reboots have not helped.

Update:

I ran this command: dpkg -l | grep postgres


rc postgresql-9.5                9.5.6-0ubuntu0.16.04                   amd64        object-relational SQL database, version 9.5 server
ii postgresql-client             9.5+173                                all          front-end programs for PostgreSQL (supported version)
ii postgresql-client-9.5         9.5.7-0ubuntu0.16.04                   amd64        front-end programs for PostgreSQL 9.5
ii postgresql-client-common      173                                    all          manager for multiple PostgreSQL client versions
ii postgresql-common             173                                    all          PostgreSQL database-cluster manager
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

eg: 

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 &lt;version&gt; &lt;cluster&gt; &lt;action&gt;
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