PostgreSQL 登录报错 psql: FATAL: Peer authentication failed for user

 

在 peer 错误前,我安装 pgsql 后使用默认用户 postgres 登录的时候报错,当我直接敲命令 # psql ,

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

顺便也一起讲下!

这是因为pgsql服务器没有运行,我只安装了 postgresql 包,没有安装 postgresql-server ,装上之后运行服务 $ sudo /etc/init.d/postgresql start 。

好了开始讲正题,运行是解决了,用户和数据库也创建好了,但是创建以及登录期间又会碰到一个常见的报错。

我尝试登录用户连接其数据库 ,

postgres@linux-tvqg:/home/hang> psql -U aann -d aann_db 
    psql: FATAL:  Peer authentication failed for user "aann"

解决方法很简单,使用和pgsql用户相同的系统用户登录,
> sudo -u aann psql -U aann -d aann_db
先来讲讲出现此错误的原因。仔细看报错信息,其中peer authentication 比较注目,很明显,错误和权限有关联。

stackoverflow上有相关答案,原址: psql: FATAL: Peer authentication failed for user “dev”

psql的连接建立于Unix Socket上默认使用peer authentication,所以必须要用和 数据库用户 相同的 系统用户 进行登录。

还有一种方法,将peer authentiction 改为 md5,并给数据库设置密码。修改配置文件,
/etc/postgresql/x.x/main/pg_hba.conf

local   all          postgres         peer
// change to
local   all          postgres          md5

重新启动pgsql,
/etc/init.d/postgresql restart

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[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 user “postgres” 。 问题: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 user “postgres”](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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值