Thursday, October 28, 2021

[SOLVED] postgresql error after installing : connection to server failed

Issue

I've installed postgresql on Ubuntu 21.04.

when I want to use it with psql command I got this error:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

update 1

I had postgres before. today I tried to use it and I got this error, so I remove everything about postgres using sudo apt remove postgresql-13 postgresql-client-13 postgresql-client-common postgresql-14 postgresql-client-14 postgresql-common and installed postgres by sudo apt install postgresql .

in tutorials, they install postgres and start using it without any configurations but I can't do that.


update 2

output of pg_lsclusters command:

Ver Cluster Port Status                Owner    Data directory              Log file
13  main    5432 down,binaries_missing postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
14  main    5433 online                postgres /var/lib/postgresql/14/main /var/log/postgresql/postgresql-14-main.log


Solution

Edit this files for solve port problem:

/etc/postgresql/14/main/postgresql.conf :

port = 5433 -> port = 5432

/etc/postgresql/13/main/postgresql.conf :

port = 5432 -> port = 5433


Open /etc/postgresql/14/main/pg_hba.conf with your preferred editor and change the following line:

local     all     postgres               peer

to this:

local     all     postgres               trust

then run sudo service postgresql restart

run psql -U postgres and run this command:

ALTER USER postgres WITH ENCRYPTED PASSWORD "#your-password" ;
CRETE USER smjt2000 WITH ENCRYPTED PASSWORD "#your-password" ;
CREATE DATABASE smjt2000 OWNER smjt2000 ;

exit the postgresql shell by entering exit

change /etc/postgresql/14/main/pg_hba.conf to this:

local     all     postgres          md5

finally run sudo service postgresql restart



Answered By - Mohammad javad