Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python can not use .pgpass while connecting to PostgreSQL

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'");

My .pgpass:

*:*:*:postgres:password

is located in /var/lib/postgresql - home directory for postgres user. However when I connect to db locally:

psql -U postgres

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
like image 716
ovod Avatar asked Feb 19 '26 03:02

ovod


1 Answers

Based on the prompt from this output:

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

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 /

Also its permissions must be restricted to readable by the owner, that is, chmod 600 $HOME/.pgpass

like image 96
Daniel Vérité Avatar answered Feb 20 '26 15:02

Daniel Vérité



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!