在 Ubuntu 上重置 PostgreSQL 的密码

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

Resetting password of PostgreSQL on Ubuntu

postgresqlunix

提问by Ritesh Mehandiratta

In Ubuntu, I installed PostgreSQL database and created a superuser for the server.

在 Ubuntu 中,我安装了 PostgreSQL 数据库并为服务器创建了一个超级用户。

If I forgot the password of the postgresql superuser, how can I reset it (the password) for that user?

如果我忘记了 postgresql 超级用户的密码,我该如何为该用户重置它(密码)?

I tried uninstalling it and then installing it again but the previously created superuser is retained.

我尝试卸载它然后再次安装它,但保留了以前创建的超级用户。

回答by Daniel Vérité

Assuming you're the administrator of the machine, Ubuntu has granted you the right to sudoto run any command as any user.
Also assuming you did not restrict the rights in the pg_hba.conffile (in the /etc/postgresql/9.1/maindirectory), it should contain this line as the first rule:

假设您是机器的管理员,Ubuntu 已授予您sudo以任何用户身份运行任何命令的权利。
还假设您没有限制pg_hba.conf文件中的权限(在/etc/postgresql/9.1/main目录中),它应该包含这一行作为第一条规则:

# Database administrative login by Unix domain socket  
local   all             postgres                                peer

(About the file location: 9.1is the major postgres version and mainthe name of your "cluster". It will differ if using a newer version of postgres or non-default names. Use the pg_lsclusterscommand to obtain this information for your version/system).

(关于文件位置:9.1是主要的 postgres 版本和main您的“集群”的名称。如果使用较新版本的 postgres 或非默认名称会有所不同。使用该pg_lsclusters命令为您的版本/系统获取此信息)。

Anyway, if the pg_hba.conffile does not have that line, edit the file, add it, and reload the service with sudo service postgresql reload.

无论如何,如果pg_hba.conf文件没有该行,请编辑文件,添加它,然后使用sudo service postgresql reload.

Then you should be able to log in with psqlas the postgres superuser with this shell command:

然后,您应该能够psql使用以下 shell 命令以 postgres 超级用户身份登录:

sudo -u postgres psql

Once inside psql, issue the SQL command:

进入 psql 后,发出 SQL 命令:

ALTER USER postgres PASSWORD 'newpassword';

In this command, postgresis the name of a superuser. If the user whose password is forgotten was ritesh, the command would be:

在此命令中,postgres是超级用户的名称。如果忘记密码的用户是ritesh,则命令为:

ALTER USER ritesh PASSWORD 'newpassword';

References: PostgreSQL 9.1.13 Documentation, Chapter 19. Client Authentication

参考资料:PostgreSQL 9.1.13 文档,第 19 章客户端身份验证

Keep in mind that you need to type postgreswith a single Sat the end

请记住,您需要在结尾处输入带有单个S 的postgres