Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use md5 authentication with psycopg2?

After two hours of reading documentation, source code and help-threads, I'm giving up. I can't get psycopg2 to authenticate with a md5-string. According to this thread I don't have to anything besides enabling md5-auth in pg_hba.conf.

This is my current pg_hba.conf:

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
local   all         all                               md5
host    all         all         127.0.0.1/32          md5
host    all         all         ::1/128               md5
host    all         all         0.0.0.0/0             md5

And I use psycopg2 like this:

psycopg2.connect(host='localhost', port=5433, user='me', password='md5xxxx').cursor()

Which gives:

psycopg2.OperationalError: FATAL:  password authentication failed for user "me"

Naturally, the given password matches with pg_authid.rolpassword.

According to pg_hba.conf I can only login using md5-auth (right?). Still, my unhashed password works fine (and hashed doesn't) and I'm unable to find any references to psycopg2 hashing it in its source code.

Help?

Thanks!

like image 845
Martijn Avatar asked Jul 21 '11 20:07

Martijn


1 Answers

Psycopg2 is a wrapper around libpq, that is, the Postgres client library, which implements this already. No need to hash you password. It will be hashed (by libpq) before being sent over the wire.


It's worth noting that libpq actually sends the MD5 sum of your password salted with your user name as well as the MD5 sum of that MD5 sum salted with a shared connection constant (see the source here). Replicating that behavior would require a bit of work.

like image 72
ig0774 Avatar answered Oct 05 '22 02:10

ig0774