Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I'm trying to work on the psql on my Vagrant machine, but I get this error:

psql: could not connect to server: No such file or directory

My server is running locally and accepting connections on 

Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Note: Vagrant 1.9.2 Box: ubuntu/trusty64, 

EDIT The Commands I have used to install and run Postgres:

sudo apt-get update

sudo apt-get install postgresql

sudo su postgres

psql -d postgres -U postgres

3 Answers

0 votes
by (36.8k points)

Even I had the same issue, related to the configuration of my pg_hba.conf file (located in /etc/postgresql/9.6/main). Kindly note that I am using version 9.6 in the Postgresql.

The error is related to some misconfiguration of Postgresql, which is causing the server to crash before it starts.

You can try these instructions:

Confirm that your Postgresql service is running, using sudo service PostgreSQL start

Run pg_lsclusters from your terminal

Check what is the cluster you are running, the output should be something like:

Version - Cluster Port Status Owner Data directory

9.6 ------- main -- 5432 online postgres /var/lib/postgresql/9.6/main

Disregard that '---' signs, as they are being used, only for alignment. The important information is the version and the cluster. You can also check whether the server is running or not in the status column.

Copy your info from the version siaplayed and the cluster, and use like so: pg_ctlcluster <version> <cluster> start, so in my case, using version 9.6 and cluster 'main', it would be pg_ctlcluster 9.6 main start

If something is wrong, then Postgresql will generate a log, that can be accessed on /var/log/postgresql/postgresql-<version>-main.log, so in my case, the full command would be 

sudo nano /var/log/postgresql/postgresql-9.6-main.log.

The output should show what is the error.

2017-07-13 16:53:04 BRT [32176-1] LOG: invalid authentication method "all"

2017-07-13 16:53:04 BRT [32176-2] CONTEXT: line 90 of configuration file "/etc/postgresql/9.5/main/pg_hba.conf"

2017-07-13 16:53:04 BRT [32176-3] FATAL: could not load pg_hba.conf

  1. Fix the errors and restart your postgresql service through sudo service postgresql restart and it should be fine.

Come and join Linux training to gain great knowledge. 

 

0 votes
by (37.3k points)

It looks like PostgreSQL is not accessible through the Unix domain socket on your Vagrant machine. Here are steps to troubleshoot and fix this issue:

1. Ensure PostgreSQL is Running

Verify that PostgreSQL is running on your Vagrant machine:

sudo systemctl status postgresql

If it’s not running, start it with:

sudo systemctl start postgresql

2. Check the PostgreSQL Socket Directory

Check whether PostgreSQL is configured to use the correct socket directory or not?

The default is /var/run/postgresql. 

Check this in the postgresql.conf file:

sudo nano /etc/postgresql/{version}/main/postgresql.conf

Look for the unix_socket_directories parameter:

unix_socket_directories = '/var/run/postgresql'

3. Verify the Socket Directory

Check whether the socket directory exists and has the correct permissions:

sudo ls -ld /var/run/postgresql

The directory should exist and be writable by the PostgreSQL user.

4. Check PostgreSQL Logs

Check PostgreSQL logs for errors related to the server startup or socket issues:

sudo tail -f /var/log/postgresql/postgresql-{version}-main.log

5. Restart PostgreSQL for changes applicable

sudo systemctl restart postgresql

6. Verify the psql Command

sudo -u postgres psql -d postgres -U postgres

7. Reinstall PostgreSQL

If none of the above steps work, try reinstalling PostgreSQL

sudo apt-get remove --purge postgresql

sudo apt-get install postgresql

0 votes
ago by (1.9k points)
edited ago by

This error usually indicates that PostgreSQL is either not running or the Unix domain socket at /var/run/postgresql/.s.PGSQL.5432 is unavailable. Below are steps to diagnose and resolve this.

Step 1: Check if PostgreSQL Service is Running

Run:

sudo service postgresql status

If the output indicates that PostgreSQL is not active, start it with:

sudo service postgresql start

Step 2: Verify PostgreSQL is Listening on Port 5432

Use this command to check if PostgreSQL is actively listening on port 5432:

netstat -plnt | grep 5432

If there’s no output, it may indicate PostgreSQL isn’t configured correctly to listen on port 5432. In this case:

Open the PostgreSQL configuration file:

sudo nano /etc/postgresql/<version>/main/postgresql.conf

Ensure the unix_socket_directories line points to /var/run/postgresql.

Save and close the file, then restart PostgreSQL with:

sudo service postgresql restart

Step 3: Confirm the Unix Socket Directory Exists

Check that /var/run/postgresql/ exists:

ls /var/run/postgresql/

If it doesn’t exist, try restarting the PostgreSQL service:

sudo service postgresql restart

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...