如何更改PostgreSQL用户的密码?


当前回答

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

sudo -u postgres psql -c "\password"

其他回答

为postgres角色设置密码

sudo -u postgres psql

您将得到如下提示:

postgres=#

将用户postgres的密码更改为PostgreSQL

ALTER USER postgres WITH ENCRYPTED PASSWORD 'postgres';

您将获得以下内容:

ALTER ROLE

为此,我们需要编辑pg_hba.conf文件。

(请随意选择编辑器来替换nano。)

sudo nano /etc/postgresql/9.5/main/pg_hba.conf

在pg_hba.conf文件中更新

查找包含以下内容的未注释行(不以#开头的行)。间距略有不同,但单词应该相同。

    local   postgres   postgres   peer

to

    local   postgres   postgres   md5

现在我们需要重新启动PostgreSQL,以便更改生效

sudo service postgresql restart

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

sudo -u postgres psql -c "\password"

使用此项:

\password

输入该用户的新密码,然后确认。如果您不记得密码,并且想更改密码,您可以以“postgres”登录,然后使用此选项:

ALTER USER 'the username' WITH PASSWORD 'the new password';

TLDR:

在许多系统中,用户的帐户通常包含句点或某种标点符号(用户:john.smith,horise.johnson)。在这些情况下,必须对上面接受的答案进行修改。更改要求用户名加上双引号。

实例

ALTER USER "username.lastname" WITH PASSWORD 'password';

理论基础:

PostgreSQL对何时使用“双引号”和何时使用“单引号”非常挑剔。通常,当提供字符串时,您将使用单引号。

对于我在Ubuntu 14.04(Trusty Tahr)上的案例,安装了PostgreSQL 10.3:我需要遵循以下步骤

su-postgres将用户切换到postgrespsql以进入PostgreSQL shell\密码,然后输入密码Q退出shell会话然后,通过执行exit并配置pg_hba.conf(我的位于/etc/postgresql/10/main/pg_hba.coff),切换回root,确保您有以下行本地所有postgres md5通过service PostgreSQL Restart重新启动PostgreSQL服务现在切换到postgres用户并再次输入PostgreSQL shell。它将提示您输入密码。