我已经安装了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"

当前回答

如果我没记错的话,默认情况下,用户postgres在Ubuntu上没有设置数据库密码。也就是说,您只能使用postgres操作系统用户帐户登录该帐户。

假设,你有根访问的盒子,你可以做:

sudo -u postgres psql

如果失败,提示数据库“postgres”不存在,那么你很可能不在Ubuntu或Debian服务器上:-)在这种情况下,只需在命令中添加template1:

sudo -u postgres psql template1

如果这些命令中的任何一个失败,出现错误psql: FATAL: password authentication failed for user "postgres",那么检查/etc/postgresql/8.4/main/pg_hba.conf文件:必须有这样的一行作为第一个非注释行:

local   all         postgres                          ident

对于较新版本的PostgreSQL, ident实际上可能是peer。这也没关系。

在psql shell中,你可以给数据库用户postgres设置一个密码:

ALTER USER postgres PASSWORD 'newPassword';

您可以通过键入CtrlD或使用\q命令离开psql shell。

现在,您应该能够为pgAdmin提供一个有效的数据库超级用户密码,它也会很高兴。: -)

其他回答

在登录postgres时,我也遇到了这个问题。我遵循以下步骤,并能够登录postgres和pgadmin。

步骤1:使用终端打开Postgres。

sudo su postgres

步骤2:打开psql。

psql

步骤3:重置用户密码

ALTER USER user_name WITH PASSWORD 'new_password';

Step4:赋予用户对数据库的权限。

GRANT ALL PRIVILEGES ON DATABASE my_database TO db_user;

编辑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方法,并重新启动。

我只是想补充一句,你也应该检查一下你的密码是否过期了。

详细信息请参见Postgres密码验证失败。

在我的例子中,Ubuntu 20.04 Postgresql 12使用了错误的端口。

我检查了/etc/postgresql/12/main/postgresql.conf,发现它是5433而不是5432。

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

# 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