Postgresql is the server running locally and accepting connection on Unix domain

This is how finally solved my issue after removing the existing postgresql version and directory

sudo apt-get --purge remove postgresql-*
sudo rm -Rf /etc/postgresql /var/lib/postgresql
sudo apt-get install postgresql

You can run the following command to check if postgresql is running:

service postgresql status

If PostgreSQL is not started you can start it with:

service postgresql start
service postgresql status

If it does not start properly, you can look at what is going on in the logs:

tail /var/log/postgresql/postgresql-9.6-main.log

You might have to run createdb to initialize a database (see https://www.postgresql.org/docs/current/static/app-createdb.html):

createdb demo

Update

If PostgreSQL is running when you do service postgresql status but you still can't connect, you should check if PostgreSQL is indeed running on port 5432 or another one with:

netstat -na | grep postgres

Update 2

Since the native packages on debian/ubuntu for PostgreSQL sucks, I advise you to install these instead: https://www.gab.lc/articles/install_postgresql_9-5_debian_ubuntu (replace 9-2 with 9-6).