Can I use md5 authentication with psycopg2?

和自甴很熟 提交于 2020-01-01 09:33:28

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/6782026/can-i-use-md5-authentication-with-psycopg2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!