如何更改 PostgreSQL 用户的密码?
要不使用密码登录:
sudo -u user_name psql db_name
忘记密码时重置密码:
ALTER USER user_name WITH PASSWORD 'new_password';
要更改 PostgreSQL 用户的密码,请按照以下步骤操作:
登录到 psql:
$ sudo -u postgres psql
然后在 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
您可以并且应该对用户的密码进行加密:
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.
psql
历史记录或shell会记住未加密的密码...如何使用散列(例如SHA1)密码?
我相信更改密码的最佳方法是简单地使用:
\password
在 Postgres 控制台中。
每 ALTER USER
documentation:
使用此命令指定未加密的密码时必须小心。密码将以明文形式传输到服务器,也可能会记录在客户端的命令历史记录或服务器日志中。 psql 包含一个命令 \password ,可用于更改角色的密码而不暴露明文密码。
注意:ALTER USER
是 ALTER ROLE
的别名
\password username
psql
提示。
要使用 Linux 命令行更改密码,请使用:
sudo -u <user_name> psql -c "ALTER USER <user_name> PASSWORD '<new_password>';"
更改密码
sudo -u postgres psql
然后
\password postgres
现在输入新密码并确认
然后 \q
退出
转到您的 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 终端
然后输入
\密码
并输入 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 , $端口, $主机);'感谢您的帮助!
为 Postgres 角色设置密码
$ sudo -u postgres psql
你会得到如下的东西:
postgres=#
将用户 Postgres 的密码更改为 Postgres
# ALTER USER postgres WITH ENCRYPTED PASSWORD 'postgres';
你会得到如下的东西:
ALTER ROLE
postgres=#
为此,我们需要编辑 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
现在我们需要重新启动 Postgres 以使更改生效。
$ sudo service postgresql restart
为 postgres 用户请求新密码(不在命令中显示):
sudo -u postgres psql -c "\password"
这是谷歌上的第一个结果,当时我正在寻找如何重命名用户,所以:
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
文件并从 md5
更改为 peer
打开 cmd,输入 psql postgres postgres
然后键入 \password
以提示输入新密码。
Refer to this medium post 了解更多信息和细化步骤。
trust
而不是 peer
对我有用
我在服务器上的配置进行了很多定制,只有在 pg_hba.conf
file 中设置 trust 身份验证后我才设法更改密码:
local all all trust
不要忘记将其改回密码或 md5
sudo systemctl restart postgresql.service
对于我在安装了 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。它会提示您输入密码。
用这个:
\password
输入该用户所需的新密码,然后确认。如果您不记得密码并且想要更改它,您可以使用 postgres 登录,然后使用:
ALTER USER 'the username' WITH PASSWORD 'the new password';
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 ;
"
将用户 postgres 的密码更改为 postgres
# ALTER USER postgres WITH ENCRYPTED PASSWORD '<NEW-PASSWORD>';
与语法上的其他答案类似,但应该知道您也可以传递密码的 md5,因此您不会传输纯文本密码。
以下是一些以纯文本形式更改用户密码的意外后果。
如果您没有 SSL 并且正在远程修改,则您正在通过网络传输纯文本密码。如果您将日志记录配置设置为记录 DDL 语句 log_statement = ddl 或更高,那么您的纯文本密码将显示在错误日志中。如果您不保护这些日志,那将是一个问题。如果您收集这些日志/ETL 并将它们显示在其他人可以访问的位置,他们最终可能会看到此密码等。如果您允许用户管理他们的密码,他们会在不知不觉中向管理员或低级别员工透露密码审查日志。
话虽如此,这里是我们如何通过构建密码的 md5 来更改用户的密码。
Postgres 将密码哈希为 md5 时,使用用户名对密码进行加盐,然后将文本“md5”添加到生成的哈希中。
例如:“md5”+md5(密码+用户名)
在 bash 中:
~$ echo -n "passwordStringUserName" | md5sum | awk '{print "md5"$1}'
md5d6a35858d61d85e4a82ab1fb044aba9d
在 PowerShell 中:
[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';
相关链接(请注意,我只会链接到最新版本的文档,因为它会更改一些旧版本,但 md5 仍然支持返回方式。)
创建角色
密码始终加密存储在系统目录中。 ENCRYPTED 关键字没有效果,但为了向后兼容而被接受。加密方法由配置参数 password_encryption 决定。如果显示的密码字符串已经是 MD5 加密或 SCRAM 加密的格式,那么无论 password_encryption 是什么,它都按原样存储(因为系统无法解密指定的加密密码字符串,因此以不同的格式对其进行加密)。这允许在转储/恢复期间重新加载加密密码。
password_encryption 的配置设置
postgres 密码认证文档
构建postgres密码md5
一般来说,只需使用 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 配置中总体上非常简单。
我使用的是 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 restart pg (service) execute psql -U postgres -h 127.0.0.1 enter (in pgctl console) alter密码为“SomePass”的用户 postgres;从 1. 恢复 pg_hba.conf
检查 pg_hba.conf
如果身份验证方法是“对等”,则客户端的操作系统用户名/密码必须与数据库用户名和密码匹配。在这种情况下,请将 Linux 用户“postgres”和数据库用户“postgres”的密码设置为相同。
有关详细信息,请参阅文档:https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
大多数答案大多是正确的,但你需要注意一些小问题。我遇到的问题是我从未设置过 postgres 的密码,所以我无法登录允许我更改密码的 SQL 命令行。这些是我成功使用的步骤(请注意,大多数或所有命令都需要 sudo/root 用户):
在您尝试连接的数据库集群的数据目录中编辑 pg_hba.conf。可以通过检查 systemd 命令行找到数据目录的文件夹,使用 systemctl status postgresql@VERSION-DB_CLUSTER 轻松获得。将 VERSION 替换为您的 PSQL 版本,将 DB_CLUSTER 替换为您的数据库集群的名称。如果它是自动创建的,这可能是主要的,例如。 postgresql@13-main。或者,我的 bash 在输入 postgresql@ 后提供了自动完成功能,因此您可以尝试这样做或在所有服务列表中查找 postgresql 服务(systemctl -a)。获得状态输出后,查找 CGroup 之后的第二个命令行,该命令行应该相当长,并以 /usr/lib/postgresql/13/bin/postgres 或类似开头(取决于版本、发行版和安装方法) .您正在寻找 -D 之后的目录,例如 /var/lib/postgresql/13/main。
可以通过检查 systemd 命令行找到数据目录的文件夹,使用 systemctl status postgresql@VERSION-DB_CLUSTER 轻松获得。将 VERSION 替换为您的 PSQL 版本,将 DB_CLUSTER 替换为您的数据库集群的名称。如果它是自动创建的,这可能是主要的,例如。 postgresql@13-main。或者,我的 bash 在输入 postgresql@ 后提供了自动完成功能,因此您可以尝试这样做或在所有服务列表中查找 postgresql 服务(systemctl -a)。获得状态输出后,查找 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@... 检查服务是否正常启动
与 psql 连接,非常重要的是,强制 psql 不要求输入密码。根据我的经验,即使服务器不在乎,它也会要求您输入密码,并且如果您的密码错误,它仍然会拒绝您的登录。这可以通过 -w 标志来完成。完整的命令行如下所示:sudo -u postgres psql -w -h 127.0.0.1 -p 5432。这里,postgres 是您的用户,您可能已经更改了它。 5432 是特定于集群的服务器的端口,如果您运行多个集群(例如我有 5434),可能会更高。
使用 \password 特殊命令更改密码。
请记住删除密码忽略解决方法并重新启动服务器以应用配置。
rm ~/.psql_history
ALTER USER myuser RENAME TO newname;
...由于某种原因,当我在谷歌搜索时,谷歌将我指向这里 :)