python在连接到PostgreSQL时无法使用.pgpass
在使用python脚本连接到postgresql db时,我无法使用.pgpass文件((( 我要连接的python脚本没有密码:python在连接到PostgreSQL时无法使用.pgpass,python,postgresql,Python,Postgresql,在使用python脚本连接到postgresql db时,我无法使用.pgpass文件((( 我要连接的python脚本没有密码: conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'"); 我的.pgpass: *:*:*:postgres:password 位于postgres用户的主目录中。 但是,当我在本地连接到db时: psql -U postgres 未询问密码。但对于
conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");
我的.pgpass:
*:*:*:postgres:password
位于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
根据此输出的提示: root@debian:/python_code/Junior/Level1/DB1/Generator_py#python 连接\u db.py 您正在以Unix用户身份运行命令
root
,因此相应的.pgpass
文件必须位于root的HOME
目录中,这意味着不是/var/lib/postgresql
,而是可能/root
或//code>
此外,其权限必须限制为所有者可读,即,chmod 600$HOME/.pgpass
该.pgpass文件必须位于启动脚本的用户的主目录中。脚本在另一台服务器上启动。如果脚本确实在另一台服务器上,则我建议您将.pgpass
文件复制到主目录UNIX用户在另一台服务器上运行脚本的过程。我处理了所有目录中的文件:结果相同。在本地它可以工作:不需要密码,但python脚本不工作(((