postgresql - 我试图将 postgresql md5 更改为 scram-sha-256,但我得到 FATAL 密码身份验证失败

标签 postgresql authentication

我正在使用 postgresql 并且作为学习的一部分,我尝试更改为登录方法以获得更安全的登录方法。例如使用 scram-sha-256 而不是 md5。我尝试将 postgresql.conf 文件中的 password_encryption 更改为 scram-sha256,并将 pg_hba.conf METHOD 也更改为 scram-sha-256,您可以在下面的配置中看到更改:

# - Authentication -

#authentication_timeout = 1min      # 1s-600s
password_encryption = scram-sha-256     # md5 or scram-sha-256
#db_user_namespace = off
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256
然后在重新启动服务器后,当出现以下错误时,我尝试使用 cmd 登录:
C:\Users\amir>psql -U postgres postgres
Password for user postgres:
psql: error: could not connect to server: FATAL:  password authentication failed for user "postgres"
我可以通过将所有内容更改为 md5 方法并忽略 postgresql.conf 文件中的 password_encryption 来解决问题。我该如何解决这个问题?我是否必须将配置更改为默认值然后尝试创建用户并为他们分配加密密码。

最佳答案

每个用户密码哈希都保存在表 pg_authid 中.它包括用于将密码转换为其散列的散列算法。
当设置 password_encryption在 postgresql.conf 中,您正在设置默认加密,即在创建用户或(重新)设置密码时使用的加密。表pg_authid不是 更新。
更改 pg_hba.conf 时,您是说只接受使用给定方法散列的密码。表pg_authid不是 更新。
doc 中有一个重要的说明:

To ease transition from the md5 method to the newer SCRAM method, if md5 is specified as a method in pg_hba.conf but the user's password on the server is encrypted for SCRAM (see below), then SCRAM-based authentication will automatically be chosen instead


所以解决办法是
  • 从现有用户开始,md5
  • 更新 postrgres.conf 以使用 scram 并重新加载配置
  • 重置用户密码:现在将在 pg_authid
  • 中保存为 scram
  • 你仍然可以在 pg_hba.conf 中使用 md5
  • 当对从 md5 到 scram 的移动感到满意时,更新 pg_hba.conf 以指定 scram 而不是 md5
  • 关于postgresql - 我试图将 postgresql md5 更改为 scram-sha-256,但我得到 FATAL 密码身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64316324/

    相关文章:

    postgresql - 连接到 Postgres 中的位置参数

    asp.net - session 在 asp.net 网站上混在一起

    postgresql - 英雄联盟 PG : Recover Write access revoked

    python - 来自 Python 的 Postgres : can I fetch N rows per time in a loop?

    sql - 连接宽表(10 个独特的列)

    java - 您如何在 spring-security 中注销所有已登录的用户?

    java - 使用 Google App Engine 的自定义 android 身份 validator

    r - RPostgreSQL 中的查询非常慢

    c# - ASP.NET Web 应用程序中的身份验证遇到问题

    java - 使用 Apache 异步 http 客户端进行 Http POST + 身份验证