mohammad javad
mohammad javad

Reputation: 33

postgresql error after installing : connection to server failed

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

Upvotes: 0

Views: 4390

Answers (2)

Daniel Ado
Daniel Ado

Reputation: 729

You can also check if postrgresql service is running. It could be in an inactive state.

○ postgresql.service - PostgreSQL RDBMS
 Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
 Active: **inactive (dead)**

To check the status of the service:

$sudo service postrgresql status

Your error is as a result of the service being inactive, hence not part of the local connection. To restart the service:

$sudo service postrgresql restart

Confirm if it is active:

$sudo service postrgresql status

● postgresql.service - PostgreSQL RDBMS
 Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
 Active: active (exited) since Tue 2022-01-18 02:13:35 EST; 2min 16s ago
Process: 167135 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 167135 (code=exited, status=0/SUCCESS)
    CPU: 3ms

Upvotes: 0

Mohammad javad
Mohammad javad

Reputation: 86

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

Upvotes: 2

Related Questions