【发布时间】:2021-12-10 07:09:18
【问题描述】:
在使用 python 脚本连接到 postgresql db 时,我无法使用 .pgpass 文件((( 我要连接的 python 脚本没有密码:
conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");
我的.pgpass:
*:*:*:postgres:password
位于 /var/lib/postgresql - postgres 用户的主目录。 但是,当我在本地连接到 db 时:
psql -U postgres
不询问密码。但是对于python会引发错误:
root@debian:/python_codes/Junior/Level1/DB1/Generator_py# python connect_db.py
Unable to connect to db...
Traceback (most recent call last):
File "connect_db.py", line 34, in connectdb
conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
OperationalError: fe_sendauth: no password supplied
【问题讨论】:
-
.pgpass 文件必须位于启动脚本的用户的主目录中。
-
脚本在另一台服务器上启动。
-
如果脚本确实在另一台服务器上,那么我建议您将
.pgpass文件复制到在另一台服务器上运行脚本的 UNIX 用户的主目录中。 -
我处理了所有目录中的文件:结果相同。在本地它可以工作:不询问密码,但python脚本不起作用(((
标签: python postgresql