如何更改PostgreSQL用户的密码?
当前回答
我使用的是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。在上面
其他回答
我使用的是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。在上面
通常,只需使用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角色设置密码
sudo -u postgres psql
您将得到如下提示:
postgres=#
将用户postgres的密码更改为PostgreSQL
ALTER USER postgres WITH ENCRYPTED PASSWORD 'postgres';
您将获得以下内容:
ALTER ROLE
为此,我们需要编辑pg_hba.conf文件。
(请随意选择编辑器来替换nano。)
sudo nano /etc/postgresql/9.5/main/pg_hba.conf
在pg_hba.conf文件中更新
查找包含以下内容的未注释行(不以#开头的行)。间距略有不同,但单词应该相同。
local postgres postgres peer
to
local postgres postgres md5
现在我们需要重新启动PostgreSQL,以便更改生效
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 ;
"
如果您在Windows上。
打开pg_hba.conf文件并从md5更改为对等。
打开cmd并键入psql-postgres-postgres。
然后键入\password以提示输入新密码。
有关更多信息和详细步骤,请参阅本中帖。
推荐文章
- 如何关闭mysql密码验证?
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?