如果忘记密码,有没有办法闯入 PostgreSQL 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/922804/
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-19 23:43:14  来源:igfitidea点击:

Is there a way to break into a PostgreSQL database if you forgot the password?

databasepostgresqlpassword-recovery

提问by Mark

I have a client that has a PostgreSQL database and he cannot remember the password that we used when the database was setup. Is there a way to recover that information so I do not have to blow away his database and start from scratch?

我有一个拥有 PostgreSQL 数据库的客户,他不记得我们在设置数据库时使用的密码。有没有办法恢复这些信息,这样我就不必破坏他的数据库并从头开始?

The database is running on a PC.

数据库在 PC 上运行。

回答by biggusjimmus

Step 1:Edit PostgreSQL config file to establish trust relationship to login without password:

第一步:编辑 PostgreSQL 配置文件,建立信任关系,免密码登录:

vi /var/lib/pgsql/data/pg_hba.conf

vi /var/lib/pgsql/data/pg_hba.conf

Old Line:

旧线路:

local all postgres password

本地所有 postgres 密码

Change it to:

将其更改为:

local all postgres trust

本地所有 postgres 信任

Step 2:Restart PostgreSQL Server:

第 2 步:重启 PostgreSQL 服务器:

service postgresql restart

服务 postgresql 重启

Step 3:Change password:

第三步:修改密码:

psql -U postgres template1 -c alter user postgres with password ‘newpassword';

psql -U postgres template1 -c 使用密码'newpassword'改变用户postgres;

Step 4:Password has been updated. Revert back the original settings of config file:

第 4 步:密码已更新。恢复配置文件的原始设置:

vi /var/lib/pgsql/data/pg_hba.conf

vi /var/lib/pgsql/data/pg_hba.conf

Old Line:

旧线路:

local all postgres trust

本地所有 postgres 信任

Change it to:

将其更改为:

local all postgres password

本地所有 postgres 密码

Step 5:Restart server and use your new password to access PostgreSQL Server.

第 5 步:重启服务器并使用您的新密码访问 PostgreSQL 服务器。

service postgresql restart

服务 postgresql 重启

Source

来源

回答by dsantaolalla

For 9.2, in windows:

对于 9.2,在 Windows 中:

Stop the service:

停止服务:

Net stop postgresql-x64-9

净停止 postgresql-x64-9

Modify the config file, change data/pg_hba.conf, basically md5 to trust:

修改config文件,改data/pg_hba.conf,基本md5信任:

host all all 127.0.0.1/32 trust

host all all ::1/128 trust

托管所有 127.0.0.1/32 信任

托管所有 ::1/128 信任

Start the service:

启动服务:

Net start postgresql-x64-9

网络启动 postgresql-x64-9

Execute the sql statement to set your desired password, on a sql console or using psql:

在 sql 控制台或使用 psql 执行 sql 语句以设置所需的密码:

alter user postgres with password ‘newpassword';

使用密码“newpassword”更改用户 postgres;

Put back the original config file.

放回原来的配置文件。