psql: error: FATAL: Peer authentication failed for user “postgres“

psql: error: FATAL: Peer authentication failed for user “postgres”

引言:在 CentOS 8 完成 PostgresQL 的安装后初始化数据库后在命令行连接到 PostgresQL 数据库时, 出现的异常问题。

操作系统环境:在这里插入图片描述
PostgresQL 版本:
在这里插入图片描述
连接命令:

psql -U postgres

出现的问题:

psql: error: FATAL:  Peer authentication failed for user "postgres"

原因:安装完 PostgresQL 后, PostgresQL 连接时的默认认证方式为 peer。官方的解释:
 The peer authentication method works by obtaining the client’s operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.
 大概意思就是使用 peer 方式的认证方式, PostgresQL 会从操作系统内核中获取当前的用户名并且作为允许连接的用户名进行认证,这种方式仅仅适用于本地连接。

由于我们这里的登录名并不是对应的用户名, 因此出现了以上认证失败的信息。

解决方案:
修改本地连接登录的认证方式:
1)找到 PostgresQLdata 目录下的 pg_hba.conf 文件。一般情况下, PostgresQL 的默认 data 问价目录在 /var/lib/ 目录下, 也可以使用以下命令找到对应位置:

sudo find / -name pg_hba.conf

 输入命令 updatedb 更新以下数据库文件即可。
2)然后编辑 pg_hba.conf 文件, 由于 CentOS 8 默认是安装了 vim 的, 因此直接使用 vim 即可:

sudo vim pg_hba.conf

在这里插入图片描述
把本地的认证方式从 peer 改为 trust

3)然后再重新启动 PostgresQL 服务:

sudo systemctl restart postgresql-13.service

4)再次输入登录命令:

psql -U postgres

这次可以直接登录进入 postgres 数据库,修改对应的用户密码:

ALTER USER 'user_name' WITH PASSWORD 'new_password';

这里的 user_name 是登录的用户名, 这里我们登录的用户名是 postgresnew_password 为修改后的密码。

5) 退出当前数据库(\q 命令), 重新编辑 pg_hba.conf 文件:
在这里插入图片描述
6)再次重新启动 PostgresQL 服务:

sudo systemctl restart postgresql-13.service 

即可输入登录命令进行正常的认证登录。

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[1\]:切换用户到test时报错: postgres=# \c - test FATAL: Peer authentication failed for user "test" Previous connection kept 。 引用\[2\]:testdb01=# \q -bash-4.2$ psql -U test -d testdb01 psql: error: FATAL: Peer authentication failed for user "test" 。 引用\[3\]:当登录postgresql遇到error,如下: > psql -U postgres psql: error: FATAL: Peer authentication failed for userpostgres” 。 问题:FATAL: Peer authentication for user "" 回答: 当出现"FATAL: Peer authentication failed for user"的错误时,这意味着对于指定的用户,使用的身份验证方法失败了。在这种情况下,可能是由于数据库配置中的身份验证方法不匹配或者没有正确设置用户的访问权限。要解决这个问题,可以尝试以下几个步骤: 1. 确保数据库配置文件中的身份验证方法与用户的身份验证方法匹配。可以通过编辑pg_hba.conf文件来更改身份验证方法。 2. 检查用户是否具有正确的访问权限。可以使用GRANT语句来授予用户所需的权限。 3. 确保输入的用户名和密码是正确的,并且没有输入错误。 如果以上步骤都没有解决问题,可能需要进一步检查数据库的配置和日志文件以获取更多信息。 #### 引用[.reference_title] - *1* *2* [PostgreSQL新建用户登录报错:FATAL: Peer authentication failed for user “test](https://blog.csdn.net/sunny05296/article/details/123519369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【PostgreSQLpsql: error: FATAL: Peer authentication failed for userpostgres”](https://blog.csdn.net/wuyy0224/article/details/126737268)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值