【笔记】PostgreSQL忘记密码

前言

PostgreSQL忘记密码

修改配置文件

  • 将密码校验方式改为无密码校验
PostgreSQL/18/data/pg_hba.conf
1
2
3
4
5
6
7
8
#local   all             all                                     scram-sha-256
local all all trust

#host all all 127.0.0.1/32 scram-sha-256
host all all 127.0.0.1/32 trust

#host all all ::1/128 scram-sha-256
host all all ::1/128 trust

修改密码

  • 重启服务后,无密码登录并修改密码
1
psql -U postgres
1
postgres=# ALTER USER postgres WITH PASSWORD '<password>';

还原配置文件

PostgreSQL/18/data/pg_hba.conf
1
2
3
4
5
local   all             all                                     scram-sha-256

host all all 127.0.0.1/32 scram-sha-256

host all all ::1/128 scram-sha-256

完成