sol*_*anv 1266
对于密码少登录:
sudo -u user_name psql db_name
Run Code Online (Sandbox Code Playgroud)
如果您忘记了重置密码:
ALTER USER user_name WITH PASSWORD 'new_password';
Run Code Online (Sandbox Code Playgroud)
小智 574
然后输入:
$ sudo -u postgres psql
Run Code Online (Sandbox Code Playgroud)
然后:
\password postgres
Run Code Online (Sandbox Code Playgroud)
然后退出psql:
\q
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,请重新配置身份验证.
编辑/etc/postgresql/9.1/main/pg_hba.conf(路径将有所不同)并更改:
local all all peer
Run Code Online (Sandbox Code Playgroud)
至:
local all all md5
Run Code Online (Sandbox Code Playgroud)
然后重启服务器:
$ sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)
ygl*_*odt 72
您可以并且应该加密用户的密码:
ALTER USER username WITH ENCRYPTED PASSWORD 'password';
Run Code Online (Sandbox Code Playgroud)
Vik*_*ing 44
我相信更改密码的最佳方法就是使用:
\password
Run Code Online (Sandbox Code Playgroud)
在Postgres控制台中.
资源:
使用此命令指定未加密的密码时必须小心.密码将以明文形式传输到服务器,也可能记录在客户端的命令历史记录或服务器日志中.psql包含一个命令\密码,可用于更改角色的密码而不暴露明文密码.
来自https://www.postgresql.org/docs/9.0/static/sql-alterrole.html.
Vaj*_*tha 32
要使用Linux命令行更改密码,请使用:
sudo -u <user_name> psql -c "ALTER USER <user_name> PASSWORD '<new_password>';"
Run Code Online (Sandbox Code Playgroud)
小智 24
sudo -u postgres psql\nRun Code Online (Sandbox Code Playgroud)\n您将收到如下提示:
\npostgres=#\nRun Code Online (Sandbox Code Playgroud)\nALTER USER postgres WITH ENCRYPTED PASSWORD \'postgres\';\nRun Code Online (Sandbox Code Playgroud)\n你会得到如下的东西:
\nALTER ROLE\nRun Code Online (Sandbox Code Playgroud)\n(随意用您选择的编辑器替换nano 。)
\nALTER USER postgres WITH ENCRYPTED PASSWORD \'postgres\';\nRun Code Online (Sandbox Code Playgroud)\n查找包含以下内容的未注释行(不以#开头的\xe2\x80\x99t 行)。间距会略有不同,但单词应该是相同的。
\nALTER ROLE\nRun Code Online (Sandbox Code Playgroud)\n到
\nsudo nano /etc/postgresql/9.5/main/pg_hba.conf\nRun Code Online (Sandbox Code Playgroud)\n local postgres postgres peer\nRun Code Online (Sandbox Code Playgroud)\n
Mur*_*ala 22
转到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
Run Code Online (Sandbox Code Playgroud)
至 :
Database administrative login by Unix domain socket
local all postgres peer
Run Code Online (Sandbox Code Playgroud)
然后通过SUDO命令重启PostgreSQL服务
psql -U postgres
您现在将进入并将看到Postgresql终端
然后进入
\password
并为Postgres默认用户输入新密码,再次成功更改密码后转到pg_hba.conf并将更改还原为"md5"
现在你将登录为
psql -U postgres
用你的新密码.
如果你们都发现任何问题,请告诉我.
Aki*_*_MJ 14
更改密码
sudo -u postgres psql
Run Code Online (Sandbox Code Playgroud)
然后
\password postgres
Run Code Online (Sandbox Code Playgroud)
现在输入新密码并确认
然后\q退出
要为postgres用户请求新密码(不在命令中显示):
sudo -u postgres psql -c "\password"
Run Code Online (Sandbox Code Playgroud)
我在我的服务器上进行的配置定制了很多,只有在我在文件中设置信任身份验证后才设法更改密码:pg_hba.conf
local all all trust
Run Code Online (Sandbox Code Playgroud)
不要忘记将其更改回密码或md5
这是谷歌的第一个结果,当我在寻找如何重命名用户时,所以:
ALTER USER <username> WITH PASSWORD '<new_password>'; -- change password
ALTER USER <old_username> RENAME TO <new_username>; -- rename user
Run Code Online (Sandbox Code Playgroud)
其他一些有助于用户管理的命令:
CREATE USER <username> PASSWORD '<password>' IN GROUP <group>;
DROP USER <username>;
Run Code Online (Sandbox Code Playgroud)
将用户移动到另一个组
ALTER GROUP <old_group> DROP USER <username>;
ALTER GROUP <new_group> ADD USER <username>;
Run Code Online (Sandbox Code Playgroud)
小智 8
您可以通过执行以下命令行代码轻松更改密码:
sudo -u postgres psql -c "ALTER USER postgres PASSWORD '<new password>'
Run Code Online (Sandbox Code Playgroud)
但是,应该注意的是,您的未加密密码仍将以明文形式显示在命令行历史记录中。
如果使用 PostgreSQL 版本 10 或更低版本,最好也ENCRYPTED显式使用该关键字。
小智 8
对于使用命令提示符的Windows:
将目录更改为/bin您的PostgreSQL安装文件夹的目录。通常我们可以使用这个命令:
C:\Program Files\PostgreSQL\<version>\bin
Run Code Online (Sandbox Code Playgroud)
以超级用户身份连接到 PostgreSQL 服务器:
psql -U postgres
Run Code Online (Sandbox Code Playgroud)
执行以下命令修改超级用户密码:
ALTER USER postgres WITH PASSWORD 'new_password';
Run Code Online (Sandbox Code Playgroud)
用这个:
\password
Run Code Online (Sandbox Code Playgroud)
为该用户输入您想要的新密码,然后确认。如果您不记得密码并想更改它,您可以以 postgres 身份登录,然后使用以下命令:
ALTER USER 'the username' WITH PASSWORD 'the new password';
Run Code Online (Sandbox Code Playgroud)
小智 6
更改 PostgreSQL 用户的密码是相当简单的任务。启动 Postgres 后,使用以下命令。
ALTER ROLE username
WITH PASSWORD 'password';
Run Code Online (Sandbox Code Playgroud)
不要将用户名写入您要更改的用户,并在写入密码的“”中写入您想要的用户的新密码。
如需进一步了解,请访问以下文章:如何更改 PostgreSQL 用户的密码
对于我在Ubuntu 14.04上安装了postgres 10.3的情况。我需要遵循以下步骤
su - postgres 切换到 postgrespsql 进入postgres shell\password 然后输入您的密码\q 退出shell会话然后,通过执行以下命令exit并配置您的pg_hba.conf(我的位置为/etc/postgresql/10/main/pg_hba.conf),切换回root用户:
local all postgres md5
service postgresql restartpostgres用户并再次输入postgres shell。它将提示您输入密码。这在语法上与其他答案类似,但应该知道您也可以传递密码的MD5哈希值,因此您不是传输纯文本密码。
\n以下是一些以纯文本方式更改用户密码会产生意外后果的场景。
\nlog_statement = ddl或更高级别,那么您的纯文本密码将显示在错误日志中。话虽如此,我们可以通过构建密码的 MD5 哈希值来更改用户的密码。
\nPostgreSQL 在将密码哈希为 MD5 时,会使用用户名对密码加盐,然后将文本“md5”添加到生成的哈希值中。
\n示例:“md5”+md5(密码+用户名)
\n在重击中:
\necho -n "passwordStringUserName" | md5sum | awk \'{print "md5"$1}\'\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nmd5d6a35858d61d85e4a82ab1fb044aba9d\nRun Code Online (Sandbox Code Playgroud)\n在 PowerShell 中:
\n[PSCredential] $Credential = Get-Credential\n\n$StringBuilder = New-Object System.Text.StringBuilder\n\n$null = $StringBuilder.Append(\'md5\');\n\n[System.Security.Cryptography.HashAlgorithm]::Create(\'md5\').ComputeHash([System.Text.Encoding]::ASCII.GetBytes(((ConvertFrom-SecureStringToPlainText -SecureString $Credential.Password) + $Credential.UserName))) | ForEach-Object {\n $null = $StringBuilder.Append($_.ToString("x2"))\n}\n\n$StringBuilder.ToString();\n\n## OUTPUT\nmd5d6a35858d61d85e4a82ab1fb044aba9d\nRun Code Online (Sandbox Code Playgroud)\n所以最后我们的ALTER USER命令看起来像
ALTER USER UserName WITH PASSWORD \'md5d6a35858d61d85e4a82ab1fb044aba9d\';\nRun Code Online (Sandbox Code Playgroud)\n相关链接(请注意,我只会链接到最新版本的文档。对于较旧的版本,它会进行一些更改,但 MD5 仍然受支持。)
\n\n\n密码始终以加密方式存储在系统目录中。ENCRYPTED 关键字没有任何作用,但为了向后兼容而被接受。加密方法由配置参数password_encryption决定。如果提供的密码字符串已经采用 MD5 加密或 SCRAM 加密格式,则无论 password_encryption 为何,都会按原样存储(因为系统无法解密指定的加密密码字符串,因此需要以不同的格式对其进行加密)。这允许在转储/恢复期间重新加载加密密码。
\n
密码加密的配置设置
\n以及使用 Bash 和 Expect 的完全自动化方式(在本示例中,我们在操作系统和 PostgreSQL 运行时级别上为新配置的 PostgreSQL 密码配置了新的 PostgreSQL 管理员):
# 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 ;
"
Run Code Online (Sandbox Code Playgroud)
域名注册地址:
在许多系统上,用户的帐户通常包含句号或某种标点符号(用户:john.smith、horise.johnson)。在这些情况下,必须对上述已接受的答案进行修改。更改要求用户名用双引号引起来。
Example:
ALTER USER "username.lastname" WITH PASSWORD 'password';
Run Code Online (Sandbox Code Playgroud)
合理的:
Postgres 对何时使用“双引号”和何时使用“单引号”非常挑剔。通常,在提供字符串时,您会使用单引号。
我使用的是 Windows(Windows Server 2019;PostgreSQL 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 trustpsql -U postgres -h 127.0.0.1alter user postgres with password 'SomePass';pg_hba.conf从上面1恢复小智 5
将用户“postgres”的密码更改为“postgres”:
# ALTER USER postgres WITH ENCRYPTED PASSWORD '<NEW-PASSWORD>';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1010854 次 |
| 最近记录: |