我正在WSL(适用于Linux的Windows子系统)的Ubuntu 18.04上运行Postgres 12数据库。
我可以使用sudo -u postgres psql
连接到数据库而没有任何问题,并且可以查看和创建新的数据库和用户。
我用CREATE USER myusername WITH PASSWORD 'mypassword';
创建了一个新用户,而psql返回CREATE ROLE
。
但是,当我尝试使用用psql -h localhost -U myusername
设置的密码作为该用户登录时,会得到psql: error: could not connect to server: FATAL: password authentication failed for user "myusername"
。
我尝试使用ALTER USER myusername WITH PASSWORD 'newpassword';
多次更改该用户的密码,而psql返回ALTER ROLE
。
我发现一种常见的解决方案,例如this post,是修改pg_hba.conf
文件。
我的pg_hba.conf
文件是:
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
我尝试将第一行和第二行都修改为md5
和ident
,并且每次都重新启动postgres服务器,但无济于事。我还尝试了完全卸载并重新安装Postgres,但该方法也不起作用。
我有什么想念的吗?我还能做些其他尝试来解决此问题吗?谢谢!
答案 0 :(得分:1)
仔细检查默认端口号上是否还有另一个PostgreSQL实例。
答案 1 :(得分:1)
为便于以后在Linux的Windows子系统上遇到此问题的人员参考,我使用命令提示符搜索正在运行的服务,并查看Postgres服务是否在sc queryex type=service state=all | find /i "postgres"
下运行。
此返回:
SERVICE_NAME: postgresql-x64-9.5
DISPLAY_NAME: postgresql-x64-9.5 - PostgreSQL Server 9.5
然后,我可以使用带有服务名称的sc stop
来停止该服务:
sc stop postgresql-x64-9.5
这解决了密码验证问题。