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

Posted on

The article will describe about how to solve the above error message. There has been a lot of article discuss this kind of error type. One of them exist in the following link. The full error message is in the following line :

user@hostname:~$ psql -Upostres  
	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"?
user@hostname:~$

As it shows in the error message, using the command ‘psql’ with the additional user parameter using ‘postgres’ fail to connect to PostgreSQL database. In order to solve the problem, the following steps are taken so that the cause of the the problem will no longer available.

1. Check the status of the PostgreSQL database service. Normally, the following command is for checking the status of the service :

systemctl status postgresql

The command depends with the service configuration of the PostgreSQL database installation. Usually, if the following output appears, the PostgreSQL service is running normally :

root@hostname:~# /etc/init.d/postgresql status
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Sun 2019-07-28 22:06:12; 1 day 13h ago
  Process: 2150 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 2150 (code=exited, status=0/SUCCESS)

Jul 28 22:06:12 hostname systemd[1]: Starting PostgreSQL RDBMS...
Jul 28 22:06:12 hostname systemd[1]: Started PostgreSQL RDBMS.
root@hostname:~# 

In the above command execution for checking the PostgreSQL database status, it is using a script exists in /etc/init.d.

2. Check the port status of the PostgreSQL database service. Normally, it listens in 5432. The following command is the command execution for checking the port status :

root@hostname:~# netstat -tulpn | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      16415/postgres      
tcp6       0      0 :::5432                 :::*                    LISTEN      16415/postgres      
root@hostname:~# 

3. Last but not least, check the privileges for connecting to PostgreSQL database server. The primary rules for accessing PostgreSQL database server exists in pg_hba.conf file. It is available in the PostgreSQL database server data folder. For an example in /opt/postgresql/data. This folder is an outcome of the ‘initdb’ command execution. For further reference on the usage of ‘initdb’, please visit the article with the title of ‘How to Activate PostgreSQL Database Service’ in this link. The file pg_hba.conf file by default will have the following content for an example :

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

As it shows in the above example, all of the connection comes from local PostgreSQL database server using any kinds of user to any kinds of database will have a ‘trust’ value infor its authentication method. In other words, there is no need any additional parameter of ‘-p’ to specify or to supply password to connect to PostgreSQL database server using any users and in the context of this article, it is ‘postgres’.

 

Leave a Reply