PostgreSQL - 如何重置用户名“postgres”密码

PostgreSQL 是一个开源的 RDBMS(关系型数据库管理系统),它不仅功能丰富、快速、轻便,而且易于使用。

以下是为用户名“postgres”重置密码的步骤:

  1. 在 pg_hba.conf 中,添加或者更改以下行。

local   all         postgres

改成

local   all         postgres                          trust sameuser
  1. 重新启动 PostgreSQL 服务以使步骤1的更改生效:
  • 在 Linux 中,
/etc/init.d/postgresql-8.3 restart
  • 在 FreeBSD 中,
/usr/local/etc/rc.d/postgres restart
  1. 在本地机器上使用用户名“postgres”登录PostgreSQL修改密码:

例如

psql -U postgres
  1. 在“postgres=#”提示下,修改用户名“postgres”密码:

例如

ALTER USER postgres with password 'secure-password';
  1. 通过执行“\q”退出PostgreSQL交互会话,退出
  2. 更改配置(我们在步骤 1 中所做的),通过在 pg_hba.conf 中将单词“trust”更改为“md5”来禁用从本地机器到 PostgreSQL 的无密码登录。

例如

local   all         postgres                          trust sameuser

改回原来的配置

local   all         postgres                          md5 sameuser
  1. 重新启动 PostgreSQL,重复步骤 2 使步骤 6 的更改生效。

  2. 使用新密码重新登录PostgreSQL:

psql -U postgres
日期:2020-06-02 22:18:18 来源:oir作者:oir