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

当前回答

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

其他回答

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

# 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

这是令人沮丧的,上面的大多数答案是正确的,但他们没有提到你必须重新启动数据库服务在pg_hba.conf文件中的更改将生效。

所以如果你做出如上所述的改变:

local all postgres ident

然后以root身份重新启动(在centos上,它类似于service service postgresql-9.2 restart) 现在您应该能够以用户postgres的身份访问数据库了

$psql
psql (9.2.4)
Type "help" for help.

postgres=# 

希望这能为postgres的新用户提供信息

古老的线程,但我在2020年浪费了半天时间来处理这个问题,所以这可能会帮助到一些人:再次检查你的postgres端口(在Ubuntu上,它在/etc/postgresql/9.5/main/postgresql.conf)。psql客户端默认使用端口5432,但在我的例子中,服务器运行在端口5433上。解决方案是在psql中指定-p选项(例如psql——host=localhost——username=user -p 5433 mydatabase)。

如果不使用——host参数,psql将通过套接字进行连接,这在我的例子中是可行的,但我的Golang应用程序(使用TCP/IP)却不行。不幸的是,错误消息是用户“user”的密码验证失败,这是误导性的。解决方法是使用url连接字符串连接端口(例如postgres://user:password@localhost:5433/mydatabase)。

我的设置是数字海洋上的Ubuntu 18.04,通过apt-get安装postgres 9.5,所以不知道为什么会发生这种情况。希望这能为您节省一些时间。

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

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

工作人员的反应是正确的,但如果你想进一步自动化可以做:

$ sudo -u postgres psql -c “ALTER USER postgres PASSWORD 'postgres' ;”

完成了!您保存了User = postgres和password = postgres。

如果你没有postgres ubuntu用户的密码,请:

$ sudo passwd postgres