简体   繁体   English

安装后postgresql错误:与服务器的连接失败

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

I've installed postgresql on Ubuntu 21.04.我已经在 Ubuntu 21.04 上安装了 postgresql。

when I want to use it with psql command I got this error:当我想将它与psql命令一起使用时,出现此错误:

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更新 1

I had postgres before.我以前有过postgres。 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 .今天我尝试使用它并出现此错误,所以我使用sudo apt remove postgresql-13 postgresql-client-13 postgresql-client-common postgresql-14 postgresql-client-14 postgresql-common删除有关 postgres 的所有内容并安装 postgres sudo apt install postgresql

in tutorials, they install postgres and start using it without any configurations but I can't do that.在教程中,他们安装 postgres 并在没有任何配置的情况下开始使用它,但我不能这样做。


update 2更新 2

output of pg_lsclusters command: pg_lsclusters命令的输出:

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

Edit this files for solve port problem:编辑此文件以解决端口问题:

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

port = 5433 -> port = 5432 port = 5433 -> port = 5432

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

port = 5432 -> port = 5433 port = 5432 -> port = 5433


Open /etc/postgresql/14/main/pg_hba.conf with your preferred editor and change the following line:使用您喜欢的编辑器打开/etc/postgresql/14/main/pg_hba.conf并更改以下行:

local     all     postgres               peer

to this:对此:

local     all     postgres               trust

then run sudo service postgresql restart然后运行sudo service postgresql restart

run psql -U postgres and run this command:运行psql -U postgres并运行以下命令:

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输入exit postgresql shell

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

local     all     postgres          md5

finally run sudo service postgresql restart最后运行sudo service postgresql restart

You can also check if postrgresql service is running.您还可以检查 postrgresql 服务是否正在运行。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM