如何更改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。它将提示您输入密码。

其他回答

我在服务器上获得的配置进行了大量定制,只有在pg_hba.conf文件中设置了信任身份验证后,我才成功更改了密码:

local   all   all   trust

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

要更改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

将用户“postgres”的密码更改为“postgress”:

# ALTER USER postgres WITH ENCRYPTED PASSWORD '<NEW-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。在上面

更改密码:

 sudo -u postgres psql

Then

\password postgres

现在输入新密码并确认。

然后\q退出。