Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: MD5 Password / Plain password

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 "[email protected]"

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

What am I not understanding?

like image 302
RalphShnelvar Avatar asked Jul 30 '17 01:07

RalphShnelvar


People also ask

What is default postgres password?

For most systems, the default Postgres user is postgres and a password is not required for authentication.

What is the default password for PostgreSQL 13?

To answer what is the default password for the PostgreSQL user, there isn't one.

What is md5 authentication in PostgreSQL?

Password authentication. The password-based authentication methods are md5 and password. These methods operate similarly except for the way that the password is sent across the connection, namely MD5-hashed and clear-text respectively. If you are at all concerned about password "sniffing" attacks then md5 is preferred.

Where PostgreSQL password are stored?

PostgreSQL database passwords are separate from operating system user passwords. The password for each database user is stored in the pg_authid system catalog. Passwords can be managed with the SQL commands CREATE ROLE and ALTER ROLE, e.g., CREATE ROLE foo WITH LOGIN PASSWORD 'secret' , or the psql command \password .


1 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())
like image 90
RCross Avatar answered Oct 21 '22 20:10

RCross