当我尝试使用像这样的代码(以“用户名”运行 python)通过 psycopg2 连接到我的 postgres 服务器时:
psycopg2.connect(database="apis_master")
我得到一个错误
psycopg2.OperationalError: FATAL: Ident authentication failed for user "username"
但是当我直接从命令行(作为用户“用户名”)运行 psql 时:
psql -d apis_master
我连接没问题。
我看不出这两种连接方式有什么不同。我是否缺少 psycopg2 的一些配置选项?
最佳答案
您的命令行用户和您的 python 用户不同。当您实例化 psycopg2 对象时,传递用户凭据:
db = psycopg2.connect(
host = 192.168.0.1, # example IP, use 'localhost' if local, otherwise the IP of the server where Postgre is located.
database = 'apis_master'
user = 'my_db_user',
password = 'my_db_password'
)
这应该可以解决您的连接问题。
关于python - Ident 连接通过 psycopg2 失败,但通过命令行工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15692437/