我尝试了5次以上,以一个简单的密码进行身份验证。失败的话,我然后重置密码并再次尝试:
owner@G700:~/cp/projFolder$ sudo -u postgres psql
psql (12.3 (Ubuntu 12.3-1.pgdg20.04+1))
Type "help" for help.
postgres=# alter user ctvmUser with password '1234';
ALTER ROLE
postgres=# \q
owner@G700:~/cp/projFolder$ psql -U ctvmUser -W -d callTrackVoipMs -a -f /home/owner/cp/projFolder/src/databaseScripts/createTables.sql;
Password:
psql: error: could not connect to server: FATAL: Peer authentication failed for user "ctvmUser"
此尝试后,我尝试复制/粘贴1234,但仍然产生相同的错误。
我怀疑还有其他问题。还有什么可以阻止我通过psql进行身份验证?
我在Lubuntu 20.04上本地运行PostgreSQL 12(并且预期的数据库交互是本地的)。
否-W标志
owner@G700:~/cp/projFolder$ psql -U ctvmUser -d callTrackVoipMs -a -f /home/owner/cp/projFolder/src/databaseScripts/createTables.sql;
psql: error: could not connect to server: FATAL: Peer authentication failed for user "ctvmUser"
更新pg_hba.conf
PG::ConnectionBad: FATAL: Peer authentication failed for user 'username' error
找不到行local all all local
:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
添加带有localhost
参数的-h标志
首先,我将这一行添加到文件pg_hba.conf
的最后
host all all 127.0.0.1/32 md5
带有localhost
的-h标志,并输出:
owner@G700:~/cp/voip_call_track_backend$ psql -U ctvmUser -d callTrackVoipMs -a -f /home/owner/cp/voip_call_track_backend/src/databaseScripts/createTables.sql -h localhost;
Password for user ctvmUser:
psql: error: could not connect to server: FATAL: password authentication failed for user "ctvmUser"
FATAL: password authentication failed for user "ctvmUser"
答案 0 :(得分:1)
您要向psql参数添加-h localhost
或-h 127.0.0.1
,以便此身份验证规则可以应用:
host all all 127.0.0.1/32 md5
(md5表示要求输入密码)。
如果省略-h选项,则默认情况下它会连接到Unix域套接字,因此连接尝试将与local all all peer
行匹配。 “对等”表示OS用户必须与数据库用户相同(由于-U ctvmUser
而并非如此),并且密码将被忽略。
或者,如果您希望使用密码对本地用户进行身份验证,请将local all all peer
替换为local all all md5
。