14

When I am running this command below:

sudo -u postgres psq

I get this error message below:

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

How can I start postgreSql and fix this error?

3
  • Have you tried sudo systemctl start postgresql and sudo -u postgres -i ? And what systemctl status postgresql says?
    – Gryu
    Jan 28, 2020 at 18:25
  • After i have done that it hangs ?
    – Jonte YH
    Jan 28, 2020 at 18:29
  • Open new terminal tab and check postgresql daemon status.
    – Gryu
    Jan 28, 2020 at 18:30

3 Answers 3

5

To start the PostgreSQL database (on Ubuntu Server, no GUI), normally you would use the pg_ctl command. Become the admin user that PostgreSQL was installed with. You probably will need to set the environment variables for PGDATA and PGPORT.

When the database is up, you should see something like this:

# pg_ctl status
pg_ctl: server is running (PID: 23890)

To start the database:

# pg_ctl start

To run psql and supply the parameters:

# psql -d {dbname} -p {pgport} -U {username} -W {password} 

Ignore the # symbols as these are just representing the command line.

2
  • 2
    Do you have an link about info how to set up pg_ctl ?
    – Jonte YH
    Jan 28, 2020 at 18:50
  • If you have set up your environment variables to access PostgreSQL, such as $PGDATA and $POSTGRESQL_HOME, there should be no need to do any setup. $POSTGRESQL_HOME is where the PostgreSQL engine is installed. $PGDATA is the folder/directory where your instance is located and where the postgresql.conf file is located. Use pg_ctl --help to see what flags are available. You can find out more at the official online documentation. postgresql.org/docs/current/app-pg-ctl.html
    – S. Nixon
    Jan 29, 2020 at 22:28
3

The situation described in the question can happen after a reboot in systems with Postgres installed. The service may fail to start or not start properly.

Since 16.04 services are managed with the systemctl tool. In most circumstances you only need to restart the service:

sudo systemctl restart postgresql.service

You can also check the current status of the service:

sudo systemctl status postgresql.service

And start it if it is down:

sudo systemctl start postgresql.service

1
  • 1
    sudo systemctl stop postgresql.service -- to stop service
    – Voldemort
    Feb 15, 2023 at 9:14
2

This command will start postgresql in ubuntu

sudo -i -u postgres psql

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .