PostgreSQL新建用户登录报错:FATAL: Peer authentication failed for user “test“


背景:PostgreSQL 新建数据库用户后切换用户报错、重新指定新用户登录也报错,但postgres用户登录没有问题。


postgres用户登录数据库
# su - postgres
-bash-4.2$ psql  -U postgres -d postgres


创建数据库用户test & 创建数据库testdb01 & 授权
postgres=# CREATE USER test WITH PASSWORD '1q2w3e';
postgres=# CREATE DATABASE testdb01 OWNER test;
postgres=# GRANT ALL PRIVILEGES ON DATABASE testdb01 TO test;

切换用户到test时报错:
postgres=# \c - test
FATAL:  Peer authentication failed for user "test"
Previous connection kept

postgres=# \c testdb01
You are now connected to database "testdb01" as user "postgres".

testdb01=# \c - test
FATAL:  Peer authentication failed for user "test"
Previous connection kept

testdb01=# \q
-bash-4.2$ psql  -U test -d testdb01
psql: error: FATAL:  Peer authentication failed for user "test"


但是以postgres用户可以正常登录:

-bash-4.2$ psql  -U postgres -d testdb01    
psql (12.9)
Type "help" for help.
testdb01=# 

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

解决方法:
将peer authentiction 改为 md5,并给数据库设置密码。修改配置文件/var/lib/pgsql/12/data/pg_hba.conf,将
local   all             all             peer

两行配置的peer改成md5,修改后的内容如下:
local   all             all             md5

生效配置:
service postgresql-12 reload

再次尝试以新用户登录成功:

-bash-4.2$ psql  -U test -d testdb01                
Password for user test: 
psql (12.9)
Type "help" for help.

testdb01=>

testdb01=> \c - postgres
Password for user postgres: 
You are now connected to database "testdb01" as user "postgres".
testdb01=# 
testdb01=# \c - test
Password for user test: 
You are now connected to database "testdb01" as user "test".
testdb01=> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunny05296

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值