在Postgres上忘记管理员密码(Windows安装),无法重置

时间:2017-11-15 15:21:38

标签: windows postgresql psql

我安装了Windows PostgreSQL。

根据一些帖子,“postgres”用户没有设置默认密码,但我无法使用空密码字符串进行连接。

我尝试连接时收到此异常:

Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"

最相关的提示是这样的: https://stackoverflow.com/a/25943227/1005607

Open  pg_hba.conf  
Change md5 -> TRUST  
then restart PgAdmin.

我尝试了并重新启动了PGAdmin但是当我尝试连接时它仍然要求我输入密码:

enter image description here

Windows中的任务管理器显示一些PostgreSQL进程正在运行。我不能把它们关掉。

我试过这个但失败了:

pg_ctl restart

ERROR: 
pg_ctl: no database directory specified and environment variable PGDATA unset

psql.exe postgres
Password: (none)
ERROR:
psql: fe_sendauth: no password supplied

如何重置用户'postgres'的默认密码?

3 个答案:

答案 0 :(得分:6)

根据AK47的回答和一些其他信息,我通过执行以下操作来修复它,

1)如果当前正在运行,请停止Postgres,命令行如下。需要提供数据' DIR。在我的情况下C:\ PostgreSQL \ data

pg_ctl -D C:\PostgreSQL\data stop

2)编辑文件pg_hba.conf(它也在\ data目录中),如下所示:

正如AK40所写,c 将所有MD5引用信任,例如

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

3)现在运行

psql -U postgres

4)在出现类型的PG命令提示符中,

ALTER USER Postgres WITH PASSWORD '<newpassword>';

5)通过输入wq enter以退出PG提示

来保存

6)现在开始Postgres

pg_ctl -D C:\PostgreSQL\data start

7)可能希望稍后在MD5 -> Trust中恢复pg_hba.conf更改。

答案 1 :(得分:3)

更新您的pg_hba.conf文件以允许受信任的本地连接

[root@server] vim pg_hba.conf
>> local all all         trust

然后重新启动PostgreSQL服务器

[user@machine] pg_ctl -D C:\PostgreSQL\data restart    (Windows)
[root@server] service postgresql restart            (Linux)

此时您可以使用本地连接以postgres用户身份连接到服务器而无需输入密码(在调用-h命令时省略psql参数将使用本地连接 - 如果您通过-h,则会与host all all 0.0.0.0/0 <method>文件中的pg_hba.conf行匹配

[root@server] psql -U postgres

然后,您可以使用psql终端中的以下命令更改postgres用户角色并将密码设置为您喜欢的任何内容

[psql] alter role postgres password <new_password>;

完成此操作后,您可以重新启动PostgreSQL服务器

[user@machine] pg_ctl -D C:\PostgreSQL\data restart     (Windows)
[root@server] service postgresql restart             (Linux)

此时您的密码应更改为新密码

答案 2 :(得分:0)

我遇到了同样的问题,我无法在Windows计算机的CLI中使用Postgres,但是我设法通过

来查找密码存储的位置。
%APPDATA%\PostgreSQL\pgpass.conf 

注意:在pgAdmin中创建服务器或数据库时,必须选择存储密码选项。

我希望这会有所帮助。谢谢。