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

Forgot Admin Password on Postgres (Windows Installation), can't reset

我安装了 Windows PostgreSQL。

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

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

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

最相关的提示是:

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

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

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' 的默认密码?

更新您的 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 用户身份连接到您的服务器,而无需输入密码(调用 psql 命令时省略 -h 参数将使用本地连接 - 如果你传递 -h 那么这将匹配你的 pg_hba.conf 文件中的行 host all all 0.0.0.0/0 <method>)

[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)

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

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

1) 如果当前 运行ning 停止 Postgres,命令行如下。需要给它 'data' 目录。在我的例子中 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 Prompt

保存

6) 现在启动 Postgres

pg_ctl -D C:\PostgreSQL\data start

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

我遇到了同样的问题,我无法在我的 windows 机器上的 CLI 中使用 Postgres,但我设法通过

找到了密码的存储位置
%APPDATA%\PostgreSQL\pgpass.conf 

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

希望对您有所帮助。谢谢