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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47311101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:59:40  来源:igfitidea点击:

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

windowspostgresqlpsql

提问by gene b.

I have a Windows PostgreSQL installation.

我有一个 Windows PostgreSQL 安装。

According to some posts, there is no default password set for the 'postgres' user yet I can't connect using an empty password string.

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

I'm receiving this exception when I try to connect:

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

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

The most relevant tip was this: https://stackoverflow.com/a/25943227/1005607

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

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

I tried that and restarted PGAdmin but it still asks me for the password when I try to connect:

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

enter image description here

在此处输入图片说明

The task manager in Windows shows some PostgreSQL processes are running. I can't switch them off.

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

I have tried this and it failed:

我试过这个,但失败了:

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

How can I reset the default password for user 'postgres'?

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

回答by gene b.

Based on AK47's answer and some additional info I fixed it by doing the following,

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

1) Stop Postgres if currently running, command line below. Need to give it the 'data' dir. In my case C:\PostgreSQL\data

1)如果当前正在运行,请停止 Postgres,下面的命令行。需要给它“数据”目录。在我的情况下 C:\PostgreSQL\data

pg_ctl -D C:\PostgreSQL\data stop

2) Edit the file pg_hba.conf(it's also in the \data dir) as follows:

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

As AK40 wrote, change all MD5 references to trust, e.g.

正如 AK40 所写,将所有 MD5 引用更改为 trust,例如

# 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) Now run

3)现在运行

psql -U postgres

4) In the PG Command Prompt that appears type,

4) 在出现的 PG 命令提示符中键入,

ALTER USER Postgres WITH PASSWORD '<newpassword>';

5) Save this by typing wqenter to exit the PG Prompt

5) 通过输入wqenter 退出 PG Prompt 来保存它

6) Now start Postgres

6) 现在启动 Postgres

pg_ctl -D C:\PostgreSQL\data start

7) Might want to revert the MD5 -> Trustchange later in the pg_hba.conf.

7) 可能想MD5 -> Trust稍后在pg_hba.conf.

回答by AK47

Update your pg_hba.conffile to allow for trusted local connections

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

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

then restart your PostgreSQL server

然后重启你的 PostgreSQL 服务器

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

at this point you can connect to your server as postgres user using a local connection without the need to enter a password (omitting the -hparameter when calling the psqlcommand will use a local connection - if you pass -hthen this will match the line host all all 0.0.0.0/0 <method>in your pg_hba.conffile)

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

[root@server] psql -U postgres

You can then alter the postgres user role and set the password to whatever you like using the following command in the psqlterminal

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

[psql] alter role postgres password <new_password>;

Once this is done you can restart your PostgreSQL server again

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

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

and at this point your password should be changed to the new password

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

回答by muwonge nicholus

I was having the same issue and I couldn't use Postgres in the CLI on my windows machine but I managed to trace down where the passwords were stored via

我遇到了同样的问题,我无法在 Windows 机器上的 CLI 中使用 Postgres,但我设法通过以下方式追踪密码的存储位置

%APPDATA%\PostgreSQL\pgpass.conf 

NB: You must have selected store password option when creating a server or database in the pgAdmin.

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

I hope this helps. Thanks.

我希望这有帮助。谢谢。