连接到PostgreSQL时python无法使用.pgpass [英] python can not use .pgpass while connecting to PostgreSQL
问题描述
使用python脚本连接到postgresql db时,我无法使用.pgpass文件((((
我的python脚本没有密码:
I can not use .pgpass file while connecting to postgresql db using python script((( My python script to connect is without password:
conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");
我的 .pgpass:
*:*:*:postgres:password
位于 / var / lib / postgresql -postgres用户的主目录
但是,当我本地连接到数据库时:
is located in /var/lib/postgresql - home directory for postgres user. However when I connect to db locally:
psql -U postgres
不要求输入密码,但是对于python错误:
no password is asked. But for python error is raised:
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
root@debian:/python_codes/Junior/Level1/DB1/Generator_py# python connect_db.py
您以Unix用户 root
的身份运行命令,因此相应的 .pgpass
文件必须位于根目录的 HOME
目录中,这意味着不是 / var / lib / postgresql
而是可能 / root
或 /
you're running the commmand as Unix user root
, so the corresponding .pgpass
file must be located in root's HOME
directory, which means not /var/lib/postgresql
but probably /root
or /
限制为所有者可读,即 chmod 600 $ HOME / .pgpass
Also its permissions must be restricted to readable by the owner, that is, chmod 600 $HOME/.pgpass
这篇关于连接到PostgreSQL时python无法使用.pgpass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!