Postgres: MD5 Password / Plain password

Viewed 28742

I'm trying to understand how role passwords are supposed to operate in Postgres.

https://www.postgresql.org/docs/current/static/sql-createrole.html says for ENCRYPTED / UNENCRYPTED

If the presented password string is already in MD5-encrypted format, then it is stored encrypted as-is,

So my unencrypted password is: MyPassword .

The MD5 hash of "MyPassword" is 48503dfd58720bd5ff35c102065a52d7

If I do

-- See https://www.postgresql.org/docs/9.6/static/sql-alterrole.html
ALTER ROLE "MeOhMy"
LOGIN
PASSWORD '48503dfd58720bd5ff35c102065a52d7'
;

And then attempt to use "MyPassword" when doing

  sudo -u postgres psql meohmy -h 127.0.0.1 -d meohmy_development

I, of course, first get prompted for my sudo password and then I get prompted by Postgres "Password for meohmy"

If I enter MyPassword I get

FATAL:  password authentication failed for user "ralph@dos32.com"

If I enter, instead, 48503dfd58720bd5ff35c102065a52d7 then I can sign in.

What am I not understanding?

4 Answers

To create an md5 password for PostgreSQL, the formula is:

"md5" + md5(password + username)

Here are 3 ways you can create one, where the username is "admin" and the password is "password123"...

Linux:

# echo -n "md5"; echo -n "password123admin" | md5sum | awk '{print $1}'
md53f84a3c26198d9b94054ca7a3839366d

NOTE: The -n is critical to avoid including the newline character in your hash!

MacOS:

āžœ echo -n "md5"; md5 -qs "password123admin"                                                                                                                                                                                   
md53f84a3c26198d9b94054ca7a3839366d

Python 2:

>>> import hashlib
>>> print("md5" + hashlib.md5("password123" + "admin").hexdigest())
md53f84a3c26198d9b94054ca7a3839366d

Python 3:

as above, but use binary strings

print("md5" + hashlib.md5(b"password123" + b"admin").hexdigest())

Using Postgres11 on GCP Cloud SQL. Gitlab version gitlab-ee 13.3.4 Omnibus install

# gitlab-ctl pg-password-md5 gitlab_user
Enter password:
Confirm password:

and

# echo -n <password for gitlab_user>gitlab_user | md5sum

are equivalent.

Note: My db user is gitlab_user

Related