如何更改PostgreSQL用户的密码?
当前回答
使用pgAdmin 4:
菜单对象→ 更改密码。。。
其他回答
检查文件pg_hba.conf。
如果身份验证方法为“对等”,则客户端的操作系统用户名/密码必须与数据库用户名和密码匹配。在这种情况下,将Linux用户“postgres”和DB用户“post格雷斯”的密码设置为相同。
有关详细信息,请参阅文档:19.1。pg_hba.conf文件
要使用Linux命令行更改密码,请使用:
sudo -u <user_name> psql -c "ALTER USER <user_name> PASSWORD '<new_password>';"
以及Bash和expect的完全自动化方式(在本例中,我们在OS和PostgreSQL运行时级别为新的PostgreSQL管理员提供新设置的PostgreQL密码):
# The $postgres_usr_pw and the other Bash variables MUST be defined
# for reference the manual way of doing things automated with expect bellow
#echo "copy-paste: $postgres_usr_pw"
#sudo -u postgres psql -c "\password"
# The OS password could / should be different
sudo -u root echo "postgres:$postgres_usr_pw" | sudo chpasswd
expect <<- EOF_EXPECT
set timeout -1
spawn sudo -u postgres psql -c "\\\password"
expect "Enter new password: "
send -- "$postgres_usr_pw\r"
expect "Enter it again: "
send -- "$postgres_usr_pw\r"
expect eof
EOF_EXPECT
cd /tmp/
# At this point the 'postgres' executable uses the new password
sudo -u postgres PGPASSWORD=$postgres_usr_pw psql \
--port $postgres_db_port --host $postgres_db_host -c "
DO \$\$DECLARE r record;
BEGIN
IF NOT EXISTS (
SELECT
FROM pg_catalog.pg_roles
WHERE rolname = '"$postgres_db_useradmin"') THEN
CREATE ROLE "$postgres_db_useradmin" WITH SUPERUSER CREATEROLE
CREATEDB REPLICATION BYPASSRLS
PASSWORD '"$postgres_db_useradmin_pw"' LOGIN ;
END IF;
END\$\$;
ALTER ROLE "$postgres_db_useradmin" WITH SUPERUSER CREATEROLE
CREATEDB REPLICATION BYPASSRLS
PASSWORD '"$postgres_db_useradmin_pw"' LOGIN ;
"
我认为更改密码的最佳方法是使用:
\password
在Postgres控制台中。
根据ALTER USER文档:
使用此命令。密码将在cleartext,并且它也可能记录在客户端的命令历史记录中或服务器日志。psql包含可以使用的命令\密码在不暴露明文密码的情况下更改角色的密码。
注意:ALTER USER是ALTER ROLE的别名
要更改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
推荐文章
- 对于PostgreSQL表来说,多大才算太大?
- 将一列的多个结果行连接为一列,按另一列分组
- 使用pgadmin连接到heroku数据库
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何在PostgreSQL中自动更新时间戳
- 当使用JDBC连接到postgres时,是否可以指定模式?
- SQL:从时间戳日期减去1天
- PostgreSQL删除所有内容
- 为什么PostgreSQL要对索引列进行顺序扫描?
- PostgreSQL INSERT ON冲突更新(upsert)使用所有排除的值
- 如何检查一个表是否存在于给定的模式中
- 如何将整数转换为字符串作为PostgreSQL查询的一部分?