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

当前回答

给出的答案几乎是正确的,只是缺少一些指针,我会在我的解决方案中照顾

首先确保你的用户有sudo访问权限,如果没有,你可以使用下面的命令添加你的用户作为sudo用户

sudo adduser <username> sudo

更改将在用户下次登录时生效。

i)现在转到sudo vim /etc/postgresql/<your_postgres_version>/main/pg_hba.conf文件,寻找这样的行:

local   all             postgres                                md5 #peer

评论一下。就在这一行下面,必须有一个注释行,说:

local   all             postgres                                peer

或者对于旧版本,它将是:-

local   all         postgres                          ident

取消这一行的注释。

ii)现在重新启动postgres使用任何这些命令:-

sudo /etc/init.d/postgresql restart

OR

sudo service postgresql restart

iii)现在您可以使用以下命令简单地登录到postgres:

sudo -u postgres psql

Iv)一旦你在你可以创建任何你想要的操作,在我的情况下,我想创建一个新的数据库,你可以使用下面的命令做同样的事情:

CREATE DATABASE airflow_replica;

其他回答

答案是@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

尽量不要使用-W参数,并让密码为空。有时创建用户时使用无密码。

如果不行,重置密码。有几种方法可以做到这一点,但这适用于许多系统:

$ su root
$ su postgres
$ psql -h localhost
> ALTER USER postgres with password 'YourNewPassword';

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

postgres=# \password 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提供一个有效的数据库超级用户密码,它也会很高兴。: -)

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