如何更改 PostgreSQL 用户密码?

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

How to change PostgreSQL user password?

postgresqlchange-password

提问by Saad

How do I change the password for PostgreSQL user?

如何更改 PostgreSQL 用户的密码?

回答by solaimuruganv

For password less login:

对于无密码登录:

sudo -u user_name psql db_name

To reset the password if you have forgotten:

如果您忘记了重置密码:

ALTER USER user_name WITH PASSWORD 'new_password';

回答by Clint Bugs

Then type:

然后输入:

$ sudo -u postgres psql

Then:

然后:

\password postgres

Then to quit psql:

然后退出psql

\q

If that does not work, reconfigure authentication.

如果这不起作用,请重新配置身份验证。

Edit /etc/postgresql/9.1/main/pg_hba.conf(path will differ) and change:

编辑/etc/postgresql/9.1/main/pg_hba.conf(路径会有所不同)并更改:

    local   all             all                                     peer

to:

到:

    local   all             all                                     md5

Then restart the server:

然后重启服务器:

$ sudo service postgresql restart

回答by yglodt

You can and should have the users's password encrypted:

您可以并且应该加密用户的密码:

ALTER USER username WITH ENCRYPTED PASSWORD 'password';

回答by Viktor Nordling

I believe the best way to change the password is simply to use:

我相信更改密码的最佳方法就是使用:

\password

in the Postgres console.

在 Postgres 控制台中。

Source:

来源:

Caution must be exercised when specifying an unencrypted password with this command. The password will be transmitted to the server in cleartext, and it might also be logged in the client's command history or the server log. psql contains a command \password that can be used to change a role's password without exposing the cleartext password.

使用此命令指定未加密的密码时必须小心。密码将以明文形式传输到服务器,也可能会记录在客户端的命令历史记录或服务器日志中。psql 包含一个命令 \password,可用于在不暴露明文密码的情况下更改角色的密码。

from https://www.postgresql.org/docs/9.0/static/sql-alterrole.html.

来自https://www.postgresql.org/docs/9.0/static/sql-alterrole.html

回答by Vajira Lasantha

To change password using Linux command line, use:

要使用 Linux 命令行更改密码,请使用:

sudo -u <user_name> psql -c "ALTER USER <user_name> PASSWORD '<new_password>';"

回答by Akitha_MJ

To Change Password

更改密码

 sudo -u postgres psql

then

然后

\password postgres

now enter New Password and Confirm

现在输入新密码并确认

then \qto exit

然后\q退出

回答by Murtaza Kanchwala

Go to your Postgresql Config and Edit pg_hba.conf

转到您的 Postgresql 配置并编辑 pg_hba.conf

sudo vim /etc/postgresql/9.3/main/pg_hba.conf

sudo vim /etc/postgresql/9.3/main/pg_hba.conf

Then Change this Line :

然后改变这一行:

Database administrative login by Unix domain socket
local      all              postgres                                md5

to :

到 :

Database administrative login by Unix domain socket
local   all             postgres                                peer

then Restart the PostgreSQL service via SUDO command then

然后通过 SUDO 命令重启 PostgreSQL 服务然后

psql -U postgres

psql -U postgres

You will be now entered and will See the Postgresql terminal

您现在将进入并看到 Postgresql 终端

then enter

然后输入

\password

\password

and enter the NEW Password for Postgres default user, After Successfully changing the Password again go to the pg_hba.conf and revert the change to "md5"

并输入 Postgres 默认用户的新密码,再次成功更改密码后,转到 pg_hba.conf 并将更改恢复为“md5”

now you will be logged in as

现在您将登录为

psql -U postgres

psql -U postgres

with your new Password.

使用您的新密码。

Let me know if you all find any issue in it.

如果你们都发现任何问题,请告诉我。

回答by lcnicolau

To request a new password for the postgresuser (without showing it in the command):

postgres用户请求新密码(不在命令中显示):

sudo -u postgres psql -c "\password"

回答by ruruskyi

Configuration that I've got on my server was customized a lot and I managed to change password only after I set trustauthentication in the pg_hba.conffile:

我在我的服务器上获得的配置被定制了很多,只有在我在文件中设置信任身份验证后我才设法更改密码:pg_hba.conf

local   all   all   trust

Don't forget to change this back to password or md5

不要忘记将其改回密码或 md5

回答by Salvador Dali

This was the first result on google, when I was looking how to rename a user, so:

这是谷歌上的第一个结果,当我在寻找如何重命名用户时,所以:

ALTER USER <username> WITH PASSWORD '<new_password>';  -- change password
ALTER USER <old_username> RENAME TO <new_username>;    -- rename user

A couple of other commands helpful for user management:

一些有助于用户管理的其他命令:

CREATE USER <username> PASSWORD '<password>' IN GROUP <group>;
DROP USER <username>;

Move user to another group

将用户移动到另一个组

ALTER GROUP <old_group> DROP USER <username>;
ALTER GROUP <new_group> ADD USER <username>;