如何更改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
其他回答
转到PostgreSQL配置并编辑文件pg_hba.conf:
sudo vim/etc/postgresql/9.3/main/pg-hba.conf
然后更改此行:
Database administrative login by Unix domain socket
local all postgres md5
to:
Database administrative login by Unix domain socket
local all postgres peer
然后通过“sudo”命令重新启动PostgreSQL服务。然后
psql-U postgres
现在您将进入并看到PostgreSQL终端。
然后输入
\密码
并为PostgreSQL默认用户输入新密码。再次成功更改密码后,转到pg_hba.conf并将更改还原为“md5”。
现在您将以身份登录
psql -U postgres
使用新密码。
这是谷歌上的第一个结果,当时我正在研究如何重命名用户,所以:
ALTER USER <username> WITH PASSWORD '<new_password>'; -- change password
ALTER USER <old_username> RENAME TO <new_username>; -- rename user
其他一些有助于用户管理的命令:
CREATE USER <username> PASSWORD '<password>' IN GROUP <group>;
DROP USER <username>;
将用户移动到其他组
ALTER GROUP <old_group> DROP USER <username>;
ALTER GROUP <new_group> ADD USER <username>;
检查文件pg_hba.conf。
如果身份验证方法为“对等”,则客户端的操作系统用户名/密码必须与数据库用户名和密码匹配。在这种情况下,将Linux用户“postgres”和DB用户“post格雷斯”的密码设置为相同。
有关详细信息,请参阅文档:19.1。pg_hba.conf文件
要更改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
使用此项:
\password
输入该用户的新密码,然后确认。如果您不记得密码,并且想更改密码,您可以以“postgres”登录,然后使用此选项:
ALTER USER 'the username' WITH PASSWORD 'the new password';
推荐文章
- 如何关闭mysql密码验证?
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?