我已经安装了PostgreSQL 8.4, Postgres客户端和Pgadmin 3。控制台客户端和Pgadmin用户“postgres”身份验证失败。我已经输入用户作为“postgres”和密码“postgres”,因为它以前工作过。但是现在身份验证失败。我以前做过几次都没有这个问题。我该怎么办?发生了什么?

psql -U postgres -h localhost -W
Password for user postgres: 
psql: FATAL:  password authentication failed for user "postgres"
FATAL:  password authentication failed for user "postgres"

当前回答

当你安装postgresql时,没有为postgres用户设置密码,你必须在Unix上使用命令显式地设置它:

sudo passwd postgres

它会询问您的sudo密码,然后提示您输入新的postgres用户密码。 源

其他回答

以下是我尝试登录的一些组合:

# login via user foo
psql -Ufoo -h localhost

sudo -u postgres psql postgres

# user foo login to postgres db
psql -Ufoo -h localhost -d postgres

进入postgres shell后,输入以下命令

postgres=# \password postgres

输入此命令后,将提示您设置密码,只需设置密码,然后尝试。

我在Windows 10上也遇到了同样的错误。在我的例子中,当我设置Postgres时,我的用户名默认是Postgres。 但是当我运行命令psql时,它显示我的用户名为jitender,这是我的机器名,我不知道为什么这个用户名已经设置。

为了解决这个问题,我做了以下步骤: 执行命令psql——help

在输出中,寻找Connection Option,在这里您将看到默认用户,在我的示例中为jitender。 您还将获得设置另一个用户名的命令,该命令应该是psql——username postgres。你可以设置你需要的用户名,问题就解决了。

答案是@diego

我想补充一些关于我如何修复错误的解释,我希望它能帮助到其他人: postgres用户密码验证失败

在窗口


确保你下载Postgres软件,安装它,创建和确认密码 并确保它不复杂的一些符号和字符。 打开窗口,单击SQL Shell (PSQL),进入并创建数据库 创建连接字符串,如 postgres: / / postgres: your_password@localhost:港口/ your_database

在WSL


遵循微软文档

安装成功后

 // Open postgres
 su postgres
 
 // Type psql and hit enter
 psql

 // Create a user postgres if not exist or any other user you want 
 CREATE USER your_user_db WITH PASSWORD 'match_password_with_db_password';
 

 // Give user password same as the one you set up for postgres db
 ALTER USER your_user_db WITH PASSWORD 'match_password_with_db_password';

 // Restart the server
 sudo service postgresql restart

编辑pg_hba.conf文件,Debian在/etc/postgresql/9.3/main/pg_hba.conf, Red Hat/IBM在/var/lib/pgsql/9.4/data/pg_hba.conf

将所有身份验证方法更改为信任。 修改Linux postgres用户密码。 重新启动服务器。 使用psql -h localhost -U postgres登录,并使用刚才设置的Unix密码。 如果它工作,你应该重新设置pg_hba.conf文件的值md5或ident方法,并重新启动。