如何更改PostgreSQL用户的密码?


当前回答

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

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

要在没有密码的情况下登录,请执行以下操作:

sudo -u user_name psql db_name

如果您忘记了重置密码:

ALTER USER user_name WITH PASSWORD 'new_password';

使用此项:

\password

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

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