15

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?

Gryu
  • 7,559
  • 9
  • 33
  • 52
Jonte YH
  • 1,961

3 Answers3

6

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.

S. Nixon
  • 402
  • 2
    Do you have an link about info how to set up pg_ctl ? – Jonte YH Jan 28 '20 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. https://www.postgresql.org/docs/current/app-pg-ctl.html – S. Nixon Jan 29 '20 at 22:28
4

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

Luís de Sousa
  • 13,227
  • 26
  • 81
  • 128
2

This command will start postgresql in ubuntu

sudo -i -u postgres psql
midhun
  • 21