connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed

Working with PostgreSQL, this is common error but solution is simple as it is. In this post, I will explore what this error means and provide steps to resolve it.

postgresql database error
postgresql database error

The problem: There might have several causes

  1. Case Sensitivity Issues: PostgreSQL is case-sensitive by default. Make sure the database name is typed in the correct case.
  2. Incorrect User Permissions: Ensure the user trying to connect to the database has the necessary permissions. To verify, connect to the PostgreSQL server as a superuser (like postgres) and list all databases and their owners.
  1. Multiple PostgreSQL Instances:If there are multiple versions of PostgreSQL installed, ensure to connect to the correct instance. Run psql --version to verify that the psql client points to the correct server version.
  2. Misconfigured Connection Parameters:Verify that the PostgreSQL process has the correct file system permissions to access the database files. Check the system logs for any file system-related errors that could prevent access.
  3. Database Corruption or System Catalog Issues
  4. File System Permissions
  5. PostgreSQL Service Status:Ensure that the PostgreSQL service is running. You can check its status using:

Steps to solve the problem:

Step 1: To solve this just check .s.PGSQL.5432 file owner and permission.

To check run the following command in terminal.


root@howdosolve:~# ls -lah /var/run/postgresql/
total 8.0K
drwxrwsr-x 2 postgres postgres 100 Aug 27 13:57 .
drwxr-xr-x 25 root root 760 Aug 27 18:37 ..
-rwxrwxrwx 1 root postgres 0 Aug 27 18:36 .s.PGSQL.5432
-rw------- 1 postgres postgres 69 Aug 27 18:36 .s.PGSQL.5432.lock
-rw-r--r-- 1 postgres postgres 5 Aug 27 13:46 16-main.pid

Look at the line .s.PGSQL.5432. the owner of the file is root. change this ownership to postgres.

root@howdosolve:~# chown -R postgres /var/run/postgresql/.s.PGSQL.5432

Step 2:To change the permission run following command

root@howdosolve:~# chmod -R 777 postgres /var/run/postgresql/.s.PGSQL.5432

 

Finally restart the postgresql database.