如何更改PostgreSQL用户的密码?


当前回答

为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

其他回答

要更改PostgreSQL用户的密码,请执行以下步骤:

登录psql控制台:sudo-u postgres psql然后在psql控制台中,更改密码并退出:postgres=#\密码postgres输入新密码:<新密码>postgres=#\q

或者使用查询:

ALTER USER postgres PASSWORD '<new-password>';

或在一行中

sudo -u postgres psql -c "ALTER USER postgres PASSWORD '<new-password>';"

注:

如果这不起作用,请通过编辑/etc/postgresql/9.1/main/pg_hba.conf重新配置身份验证(路径将不同)并更改:

local     all         all             peer # change this to md5

to

local     all         all             md5 # like this

然后重新启动服务器:

sudo service postgresql restart

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

ALTER USER username WITH ENCRYPTED PASSWORD 'password';

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

sudo -u postgres psql -c "\password"

如果您在Windows上。

打开pg_hba.conf文件并从md5更改为对等。

打开cmd并键入psql-postgres-postgres。

然后键入\password以提示输入新密码。

有关更多信息和详细步骤,请参阅本中帖。

我使用的是Windows(Windows Server 2019;PostgreSQL 10),因此不支持本地类型连接(pg_hba.conf:本地所有对等)。

以下各项应适用于Windows和Unix系统:

将pg_hba.conf备份到pg_hba.org.conf,例如。仅使用以下命令创建pg_hba.conf:host all all 127.0.0.1/32信任重新启动pg(服务)执行psql-U postgres-h 127.0.0.1输入(在pgctl控制台中)使用密码“SomePass”更改用户postgres;从1还原pg_hba.conf。在上面