How can I solve the issue: psql cannot connect to the server in Ubuntu?

In hosting my Django web application on VPS using PostgreSQl. However, the configuration seems to be fine but whenever I want to access the Postgres Shell, I ma getting the error bellow.

root@vmi851374:~# sudo su -l postgres
        postgres@vmi851374:~$ psql
        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"?
        postgres@vmi851374:~$

enter image description here
I have checked the Postgres service status and everything seems fine but I don't know where this error comes from. Notice that in my localhost (laptop), I am not facing this issue. Only in the VPS server, I am getting this issue.

Please assist me to solve this issue.

Doing:

sudo systemctl status postgresql.service

just shows whether the status of the bootstrap process postgresql.service ran. This process is used to start the actual Postgres server or servers.

To see whether the actual servers are running do:

pg_lsclusters

This will show the status of the server(s).

Another option is to to use:

sudo systemctl status postgresql@<version>-main.service

where you replace <version> with the version number of the Postgres server.

I solve the problem by editing the file postgresql.conf located in /etc/postgresql/14/main/postgresql.conf Then I uncommented and edit the listen_addresses attribute to start listening to start listening to all available IP addresses: listen_addresses = '*'

In addition I edited the PostgreSQL access policy configuration file as follow:

vim /etc/postgresql/14/main/pg_hba.conf. 

Then I modified the file as follow:

host all all 0.0.0.0/0 md5

And now it is working fine. Thanks a lot @AdrianKlaver

Back to Top