python在连接到PostgreSQL时不能使用.pgpass

2024-03-28 14:38:42 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python脚本连接到postgresql数据库时无法使用.pgpass文件((( 我要连接的python脚本没有密码:

conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");

我的.pgpass:

^{pr2}$

位于postgres用户的/var/lib/postgresql-主目录中。 但是,当我在本地连接到数据库时:

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

Tags: py脚本数据库host密码dbpostgresqllib
1条回答
网友
1楼 · 发布于 2024-03-28 14:38:42

根据此输出的提示:

root@debian:/python_codes/Junior/Level1/DB1/Generator_py# python connect_db.py

您以Unix用户root的身份运行命令,因此相应的.pgpass文件必须位于根目录的HOME目录中,这意味着不是/var/lib/postgresql,而是/root或{}

它的权限也必须限制为所有者可读,即chmod 600 $HOME/.pgpass

相关问题 更多 >