更改 postgres 超级用户的身份验证方法

Change authentication method for postgres superuser

我正在使用 psql 连接到 Debian 10 上的 PostgreSQL 数据库。我正在尝试以 postgres 用户身份连接到默认的 postgres 数据库。默认情况下,这是使用不需要密码的 'peer' 身份验证方法。

如果我使用 'peer' 身份验证登录并使用以下命令设置密码:

ALTER USER postgres WITH PASSWORD 'myPassword';

查询成功执行,但是当我编辑 pg_hba.conf 以更改身份验证方法时:

local    all            postgres                    peer

至:

local    all            postgres                    scram-sha-256

并重新启动服务器,出现以下错误:

~$ sudo -u postgres psql postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
~$

有人知道怎么做吗?

要更改 PostgreSQL 中的身份验证方法:

  1. 打开一个终端window

  2. 进入postgres bin目录

示例:cd /usr/local/pgsql/bin

注意:根据您的安装环境,bin 目录的路径可能会有所不同。

  1. 键入 su – postgres 并按 Enter。这会将登录更改为 postgres 用户。

  2. 从bin目录输入./psql

  3. 类型: ALTER USER your_username password 'new_password'; 并按 Enter。 ALTER ROLE 应显示。

  4. 键入 \q 并按 Enter

  5. 打开/path_to_data_directory/pg_hba.conf

示例:/etc/postgresql/11/main/pg_hba.conf

  1. 修改配置文件底部的行以类似于这些示例之一。

注意:您可能只需要将单词 trust 更改为 md5。一行或多行应该已经存在。

host     all        postgres                                   peer 
host     all        your_username      your.ip your.subnet     md5
  1. 保存更改

  2. 重新启动 PostgreSQL 服务 systemctl restart postgresql.service

您需要在 shell 中第一个更改为“postgres”用户,您在上面没有正确地做:

sudo su - postgres

然后你可以作为peer auth做以下事情: psql -d postgres -U postgres

还建议您为 postgres 设置密码 sql 用户:

\密码 postgres & 将身份验证方法更改为“md5”,而不是“peer”。

在分配密码之前,您可能需要将 password_encryption 设置为“scram-sha-256”。不然你把密码存成md5格式,这样的密码在pg_hba.conf调用"scram-sha-256"时是不能登录的。

password_encryption的默认设置仍然是md5。它将在 v14 中更改为“scram-sha-256”。

发送给未经身份验证的用户的错误消息故意含糊不清。服务器日志文件中的错误消息可能会说 DETAIL: User "postgres" does not have a valid SCRAM secret.(如果不是,则忽略此答案,并编辑您的问题以告诉我们它说的是什么)