【发布时间】:2012-09-25 02:49:00
【问题描述】:
如何更改 PostgreSQL 用户的密码?
【问题讨论】:
标签: postgresql change-password
如何更改 PostgreSQL 用户的密码?
【问题讨论】:
标签: postgresql change-password
无密码登录:
sudo -u user_name psql db_name
忘记密码时重置密码:
ALTER USER user_name WITH PASSWORD 'new_password';
【讨论】:
rm ~/.psql_history
ALTER USER myuser RENAME TO newname; ...由于某种原因,当我在谷歌上搜索时,谷歌将我指向这里:)
将用户 postgres 的密码更改为 postgres
# ALTER USER postgres WITH ENCRYPTED PASSWORD '<NEW-PASSWORD>';
【讨论】:
$ sudo -u postgres psql
你会得到类似如下的东西:
postgres=#
# ALTER USER postgres WITH ENCRYPTED PASSWORD 'postgres';
你会得到类似如下的东西:
ALTER ROLE
postgres=#
=====> 随意将 nano 替换为您选择的编辑器
$ sudo nano /etc/postgresql/9.5/main/pg_hba.conf
$ sudo service postgresql restart
【讨论】:
要更改 postgres 用户的密码,请按照以下步骤操作
$ sudo -u postgres psql
postgres=# \password postgres
Enter new password: <new-password>
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
【讨论】:
\p,它会给我密码;如果我输入\password postgres,它会给出密码,然后警告\p extra argument "assword" ignored; \p extra argument "postgres" ignored
sudo passwd postgres
大多数答案大多是正确的,但您需要注意一些小问题。我遇到的问题是我从未设置过 postgres 的密码,所以我无法登录允许我更改密码的 SQL 命令行。这些是我成功使用的步骤(注意大部分或所有命令都需要 sudo/root 用户):
pg_hba.conf。
systemctl status postgresql@VERSION-DB_CLUSTER很容易获得。将 VERSION 替换为您的 PSQL 版本,将 DB_CLUSTER 替换为您的数据库集群的名称。如果它是自动创建的,这可能是主要的,例如。 postgresql@13-main。或者,我的 bash 在输入 postgresql@ 后提供了自动完成功能,因此您可以尝试这样做或在所有服务列表 (systemctl -a) 中查找 postgresql 服务。获得状态输出后,查找 CGroup 之后的第二个命令行,该命令行应该相当长,并以 /usr/lib/postgresql/13/bin/postgres 或类似名称开头(取决于版本、发行版和安装方法)。您正在寻找-D 之后的目录,例如/var/lib/postgresql/13/main。host all all 127.0.0.1/32 trust。这允许所有数据库上的所有用户无条件地通过本地计算机上的 IPv4 连接到数据库,而无需输入密码。这是一个临时修复,不要忘记稍后再次删除此行。为了确定起见,我注释掉了host all all 127.0.0.1/32 md5(md5可能被scram-sha-256替换),它对相同的登录数据有效,只需要密码。systemctl restart postgresql@... 再次使用您之前找到的服务。systemctl status postgresql@... 正确启动。-w 标志来完成。完整的命令行如下所示:sudo -u postgres psql -w -h 127.0.0.1 -p 5432。在这里,postgres 是您的用户,您可能已经更改了它。 5432 是集群特定服务器的端口,如果您运行多个集群(例如我有 5434),可能会更高。\password 特殊命令更改密码。【讨论】:
我使用的是 Windows(Server 2019;PG 10),因此不支持 local 类型的连接(pg_hba.conf:local all all peer)。
以下应该适用于 Windows 和 Unix 系统:
pg_hba.conf 到pg_hba.orig.conf 例如pg_hba.conf:host all all 127.0.0.1/32 trust
psql -U postgres -h 127.0.0.1
alter user postgres with password 'SomePass';
pg_hba.conf
【讨论】:
如果你在 Windows 上。
打开pg_hba.conf文件并将md5更改为peer
打开cmd,输入psql postgres postgres
然后键入\password 以提示输入新密码。
Refer to this medium post 了解更多信息和详细步骤。
【讨论】:
trust而不是peer对我有用
检查 pg_hba.conf
如果认证方式为“peer”,客户端的操作系统用户名/密码必须与数据库用户名和密码匹配。在这种情况下,请将 Linux 用户“postgres”和 DB 用户“postgres”的密码设置为相同。
详见文档:https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
【讨论】:
我相信更改密码的最好方法就是使用:
\password
在 Postgres 控制台中。
每ALTER USERdocumentation:
在指定未加密的密码时必须小心 这个命令。密码将被传送到服务器 明文,它也可能记录在客户端的命令历史记录中 或服务器日志。 psql 包含一个可以使用的命令\password 在不暴露明文密码的情况下更改角色的密码。
注意:ALTER USER 是 ALTER ROLE 的别名
【讨论】:
\password username
psql 提示符下。
TLDR:
在许多系统上,用户的帐户通常包含句点或某种标点符号(用户:john.smith、horise.johnson)。在这些情况下,必须对上面接受的答案进行修改。更改要求用户名双引号。
Example:
ALTER USER "username.lastname" WITH PASSWORD 'password';
理性:
Postgres 对何时使用“双引号”和何时使用“单引号”非常挑剔。通常在提供字符串时,您会使用单引号。
【讨论】:
以及使用 bash 和 expect 的全自动方式(in this example 我们在 OS 和 postgres 运行时级别上使用新配置的 postgres pw 配置了一个新的 postgres 管理员)
# the $postgres_usr_pw and the other bash vars 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 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 ;
"
【讨论】:
一般来说,只需使用 pg admin UI 来进行 db 相关的活动。
如果您更专注于为本地开发或 CI 等自动化数据库设置...
例如,您可以使用这样的简单组合。
(a) 通过 jenkins 创建一个虚拟超级用户,命令类似于:
docker exec -t postgres11-instance1 createuser --username=postgres --superuser experiment001
这将在您的 postgres 数据库中创建一个名为 Experiment001 的超级用户。
(b) 通过运行非交互式 SQL 命令为该用户提供一些密码。
docker exec -t postgres11-instance1 psql -U experiment001 -d postgres -c "ALTER USER experiment001 WITH PASSWORD 'experiment001' "
Postgres 可能是用于命令行(非交互式)工具的最佳数据库。 创建用户、运行 SQL、备份数据库等... 一般来说,postgres 都是非常基础的,将其集成到您的开发设置脚本或自动化 CI 配置中总体上非常简单。
【讨论】:
在语法上与其他答案类似,但应该知道您也可以传递密码的 md5,因此您不会传输纯文本密码。
以下是一些以纯文本形式更改用户密码的意外后果。
log_statement = ddl 或更高版本,那么您的纯文本密码将显示在错误日志中。
说到这里,我们可以通过构建密码的 md5 来更改用户的密码。
例如:“md5”+md5(密码+用户名)
在 bash 中:
~$ echo -n "passwordStringUserName" | md5sum | awk '{print "md5"$1}'
md5d6a35858d61d85e4a82ab1fb044aba9d
[PSCredential] $Credential = Get-Credential
$StringBuilder = New-Object System.Text.StringBuilder
$null = $StringBuilder.Append('md5');
[System.Security.Cryptography.HashAlgorithm]::Create('md5').ComputeHash([System.Text.Encoding]::ASCII.GetBytes(((ConvertFrom-SecureStringToPlainText -SecureString $Credential.Password) + $Credential.UserName))) | ForEach-Object {
$null = $StringBuilder.Append($_.ToString("x2"))
}
$StringBuilder.ToString();
## OUTPUT
md5d6a35858d61d85e4a82ab1fb044aba9d
ALTER USER 命令看起来像 ALTER USER UserName WITH PASSWORD 'md5d6a35858d61d85e4a82ab1fb044aba9d';
密码始终以加密方式存储在系统目录中。 ENCRYPTED 关键字没有效果,但为了向后兼容而被接受。加密方法由配置参数 password_encryption 决定。如果显示的密码字符串已经是 MD5 加密或 SCRAM 加密的格式,那么无论 password_encryption 是什么,它都按原样存储(因为系统无法解密指定的加密密码字符串,因此以不同的格式对其进行加密)。这允许在转储/恢复期间重新加载加密密码。
【讨论】:
修改密码
sudo -u postgres psql
然后
\password postgres
现在输入新密码并确认
然后\q 退出
【讨论】:
要使用 Linux 命令行更改密码,请使用:
sudo -u <user_name> psql -c "ALTER USER <user_name> PASSWORD '<new_password>';"
【讨论】:
对于我在安装了 postgres 10.3 的 Ubuntu 14.04 上的情况。我需要按照以下步骤进行
su - postgres 切换用户到postgres
psql进入postgres shell\password然后输入你的密码\q 退出 shell 会话然后你通过执行exit 切换回root 并通过确保你有以下行来配置你的pg_hba.conf(我的是/etc/postgresql/10/main/pg_hba.conf)
local all postgres md5
service postgresql restart 重新启动您的 postgres 服务
postgres用户并再次进入postgres shell。它会提示您输入密码。【讨论】:
为 postgres 用户请求新密码(不在命令中显示):
sudo -u postgres psql -c "\password"
【讨论】:
使用这个:
\password
输入该用户所需的新密码,然后确认。 如果你不记得密码并且想更改它,你可以使用 postgres 登录,然后使用这个:
ALTER USER 'the username' WITH PASSWORD 'the new password';
【讨论】:
您可以并且应该对用户的密码进行加密:
ALTER USER username WITH ENCRYPTED PASSWORD 'password';
【讨论】:
The password is always stored encrypted in the system catalogs. The ENCRYPTED keyword has no effect, but is accepted for backwards compatibility.
psqlhistory 会记住未加密的密码或shell...如何使用散列(例如SHA1)密码?
【讨论】:
sudo systemctl restart postgresql.service
这是谷歌上的第一个结果,当时我正在寻找如何重命名用户,所以:
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>;
【讨论】:
转到您的 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
到:
Database administrative login by Unix domain socket
local all postgres peer
然后通过 SUDO 命令重启 PostgreSQL 服务
psql -U postgres
您现在将进入并看到 Postgresql 终端
然后输入
\password
并为 Postgres 默认用户输入新密码,在成功更改密码后再次转到 pg_hba.conf 并将更改恢复为“md5”
现在您将以
身份登录psql -U postgres
使用您的新密码。
如果大家发现其中有任何问题,请告诉我。
【讨论】:
user@user-NC10:~$ psql -U postgres psql: FATAL: Peer authentication failed for user "postgres"
$version,' /usr/bin/psql: psql: line 26: my ( $version, $cluster, $db, $port, $host);'感谢您的帮助!