如何查找或重置Postgresql psql postgres用户密码?

问题描述 投票:0回答:1

我正在尝试进入我的 postgres shell 来管理我的数据库,但在进入我的 postgres shell 时遇到了问题。

omars-mbp:postgres omarjandali$ brew services restart postgres
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
omars-mbp:postgres omarjandali$ psql
Password for user omarjandali: 
psql: error: could not connect to server: FATAL:  password authentication failed for user "omarjandali"

我还尝试了默认的 psql postgres 用户:

omars-mbp:postgres omarjandali$ psql -U postgres
Password for user postgres: 
psql: error: could not connect to server: FATAL:  password authentication failed for user "postgres"
omars-mbp:postgres omarjandali$ 

我有办法重置密码吗?我不记得设置过主密码。

postgresql psql
1个回答
4
投票

步骤 1. 通过将

pg_hba.conf
文件复制到其他位置来备份它,或者仅将其复制到例如
pg_hba.conf.bk

步骤 2. 编辑

pg_dba.conf
文件并将所有本地连接的最后一列从
scram-sha-256
md5
更改为
trust
。通过这样做,您将能够在不使用密码的情况下登录 PostgreSQL 数据库服务器。

步骤 3. 重新启动 PostgreSQL 服务器(服务)。

步骤 4. 使用 psql 或 pgAdmin 等任何工具连接到 PostgreSQL 数据库服务器:

psql -U postgres

PostgreSQL 不需要密码即可登录。

第5步。执行以下命令为postgres用户设置新密码。

ALTER USER postgres WITH PASSWORD 'new_password';

PostgreSQLTutorial

提供
© www.soinside.com 2019 - 2024. All rights reserved.