如何更改PostgreSQL用户的密码?
当前回答
要在没有密码的情况下登录,请执行以下操作:
sudo -u user_name psql db_name
如果您忘记了重置密码:
ALTER USER user_name WITH PASSWORD 'new_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
以及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 ;
"
将用户“postgres”的密码更改为“postgress”:
# ALTER USER postgres WITH ENCRYPTED PASSWORD '<NEW-PASSWORD>';
更改密码:
sudo -u postgres psql
Then
\password postgres
现在输入新密码并确认。
然后\q退出。
通常,只需使用pgAdmin UI执行与数据库相关的活动。
如果相反,您更专注于为本地开发、CI等自动化数据库设置。
例如,可以使用这样的简单组合。
(a) 通过Jenkins创建一个虚拟超级用户,命令如下:
docker exec -t postgres11-instance1 createuser --username=postgres --superuser experiment001
这将在PostgreSQL数据库中创建一个名为experience001的超级用户。
(b) 通过运行NON Interactive SQL命令为该用户提供一些密码。
docker exec -t postgres11-instance1 psql -U experiment001 -d postgres -c "ALTER USER experiment001 WITH PASSWORD 'experiment001' "
PostgreSQL可能是命令行(非交互式)工具的最佳数据库。创建用户、运行SQL、备份数据库等。
一般来说,PostgreSQL都是非常基础的,将其集成到开发设置脚本或自动化CI配置中总体来说非常简单。
推荐文章
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?
- PostgreSQL:角色不允许登录
- 如何查找Postgres / PostgreSQL表及其索引的磁盘大小
- 是使用各有一个模式的多个数据库更好,还是使用一个数据库有多个模式更好?
- 如果字符串包含
- 对于PostgreSQL表来说,多大才算太大?
- 将一列的多个结果行连接为一列,按另一列分组
- 使用pgadmin连接到heroku数据库
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?