我正在尝试了解角色密码在 Postgres 中的运作方式。
https://www.postgresql.org/docs/current/static/sql-createrole.html表示加密/未加密
If the presented password string is already in MD5-encrypted format, then it is stored encrypted as-is,
所以我未加密的密码是:MyPassword。
“我的密码”的 MD5 哈希值是 48503dfd58720bd5ff35c102065a52d7
如果我这样做
-- See https://www.postgresql.org/docs/9.6/static/sql-alterrole.html
ALTER ROLE "MeOhMy"
LOGIN
PASSWORD '48503dfd58720bd5ff35c102065a52d7'
;
然后在做的时候尝试使用“MyPassword”
sudo -u postgres psql meohmy -h 127.0.0.1 -d meohmy_development
当然,首先会提示我输入 sudo 密码,然后我会收到 Postgres 的提示“meohmy 的密码”
如果我输入 MyPassword,我会得到
FATAL: password authentication failed for user "ralph@dos32.com"
如果我输入 48503dfd58720bd5ff35c102065a52d7,那么我就可以登录了。
我不明白什么?
最佳答案
要为 PostgreSQL 创建 md5 密码,公式为:
"md5" + md5(password + username)
您可以通过以下 3 种方式创建一个,其中用户名是“admin”,密码是“password123”...
Linux:
# echo -n "md5"; echo -n "password123admin" | md5sum | awk '{print $1}'
md53f84a3c26198d9b94054ca7a3839366d
注意:-n 是避免在散列中包含换行符的关键!
MacOS:
➜ echo -n "md5"; md5 -qs "password123admin"
md53f84a3c26198d9b94054ca7a3839366d
Python 2:
>>> import hashlib
>>> print("md5" + hashlib.md5("password123" + "admin").hexdigest())
md53f84a3c26198d9b94054ca7a3839366d
Python 3:
如上,但使用二进制字符串
print("md5" + hashlib.md5(b"password123" + b"admin").hexdigest())
关于postgresql - Postgres : MD5 Password/Plain password,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45395538/