psql:错误:无法连接到服务器:致命:用户“postgres”的对等身份验证失败

Jel*_* Wu 2 postgresql

我使用这里的命令安装并设置了 postgresql-12 [ https://www.postgresql.org/download/linux/redhat/]

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql12
yum install postgresql12-server
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12
Run Code Online (Sandbox Code Playgroud)

然后我尝试登录数据库

psql postgres postgres
Run Code Online (Sandbox Code Playgroud)

我得到了错误

psql: error: could not connect to server: FATAL:  Peer authentication failed for user "postgres"
Run Code Online (Sandbox Code Playgroud)

我的系统上有两个 pg_hba.conf 文件。我检查了身份验证设置,如下所示,按照这里的建议 [ psql: FATAL: Ident authentication failed for user "postgres"

/data/pg_data_dir/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             0.0.0.0/0               md5

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
Run Code Online (Sandbox Code Playgroud)

/var/lib/pgsql/12/data/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5  
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
Run Code Online (Sandbox Code Playgroud)

身份验证设置看起来不错,所以我不确定从这里开始做什么。

adr*_*mih 5

编辑 - 使其更具结构性和可用性。

首先,你需要了解每个这样的认证方法的意思是,有一吨的infromation在这里这一点。长话短说,在这个阶段,您可能会感兴趣的是md5哪个是散列密码,或者trust哪个不是密码。

在您的机器上安装 postgres 后,您需要成为postgres用户,然后尝试psql

~# su -l postgres --assume you're postgres
~$ psql --connect

psql (xx.x ))
Type "help" for help.

postgres=#
Run Code Online (Sandbox Code Playgroud)

进入后,您可以为该用户设置新密码:

postgres=# \password postgres
Run Code Online (Sandbox Code Playgroud)

为了防止这种情况发生,您需要询问 postgres hba 文件的位置:

postgres=# SHOW hba_file;
Run Code Online (Sandbox Code Playgroud)

使用您最喜欢的文本编辑器并对其进行修改以满足您的需求。

另外你需要确保你重新加载了 postgresql 服务,命令是:

sudo systemctl reload postgresql-12.service

  • 那``sudo -u postgres psql```怎么样?这会返回相同的错误吗? (2认同)