使用 python 脚本连接到 postgresql 数据库时无法使用 .pgpass 文件((( 我的连接 python 脚本没有密码:
conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");
我的.pgpass:
*:*:*:postgres:password
位于/var/lib/postgresql - postgres 用户的主目录。 但是,当我本地连接到数据库时:
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_codes/Junior/Level1/DB1/Generator_py# python connect_db.py
您以 Unix 用户 root
身份运行该命令,因此相应的 .pgpass
文件必须位于 root 的 HOME
目录中,该目录表示不是 /var/lib/postgresql
而是可能 /root
或 /
此外,其权限必须限制为所有者可读,即 chmod 600 $HOME/.pgpass
关于python 连接到 PostgreSQL 时无法使用 .pgpass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28931828/