How to start PostgreSql

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

2 Answers

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like