14

I am trying to install postgresql-9.5 or postgresql-9.6 on my ubuntu 16.04 or 14.04 machine, after following the installation process using below commands.

sudo apt-get install update
sudo apt-get install postgresql postgresql-contrib

But when I am trying to open psql from my terminal using below commands

sudo su postgres 
psql 

every time its just showing.

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

I have tried approximately 10 stackoverflow.com solutions but no success. Please help me out before I tear all my hairs :(

$ service postgresql status

● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
   Active: active (**exited) since রবি 2017-03-12 21:45:56 BDT; 23min ago
  Process: 917 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 917 (code=exited, status=0/SUCCESS)
    Tasks: 0
   Memory: 0B
      CPU: 0
   CGroup: /system.slice/postgresql.service

see this line carefully why exited ? Active: active (exited) since রবি 2017-03-12 21:45:56 BDT; 23min ago

2
  • active (exited) is normal Commented Mar 12, 2017 at 16:20
  • First thing to check is the postgres logfile. Postgres fails to start up on (some) configuration errors, and reports this in the logfile, before exiting. Commented Mar 12, 2017 at 16:41

2 Answers 2

22

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).

Sign up to request clarification or add additional context in comments.

3 Comments

I added a port check to my answer, please let me know if it helps
netstat -na | grep postgres shows nothing on the terminal
ok I strongly advise you to remove the packages from debian and install the ones suggested in my "update 2"
8

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

1 Comment

Straightforward answer that works. Thank you for writing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.