【问题标题】:Forgot Admin Password on Postgres (Windows Installation), can't reset忘记 Postgres 上的管理员密码(Windows 安装),无法重置
【发布时间】:2023-03-14 04:07:01
【问题描述】:

我有一个 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,但当我尝试连接时它仍然要求我输入密码:

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

【问题讨论】:

    标签: windows postgresql psql


    【解决方案1】:

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

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

    pg_ctl -D C:\PostgreSQL\data stop
    

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

    正如 AK40 所写,c将所有 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) 现在运行

    psql -U postgres
    

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

    ALTER USER Postgres WITH PASSWORD '<newpassword>';
    

    5) 输入wq 保存此文件,输入退出 PG 提示

    6) 现在启动 Postgres

    pg_ctl -D C:\PostgreSQL\data start
    

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

    【讨论】:

    • 我将根据我遗漏的一些细节来更新我的答案
    • 最好准确地说所有地址为本地地址的“md5”都必须更改。信任每个连接,即使是来自服务器外部的连接也可能很危险。
    • 如果服务器停止,您希望psql -U postgres 如何工作?
    • 这就是启动服务器的原因
    【解决方案2】:

    更新您的 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 参数将使用本地连接 - 如果您通过 @ 987654327@ 那么这将与您的pg_hba.conf 文件中的host all all 0.0.0.0/0 &lt;method&gt; 行匹配)

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

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

    【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2017-01-21
      • 1970-01-01
      • 2013-06-03
      • 2021-12-26
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 2020-05-07
      相关资源
      最近更新 更多