Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ident connection fails via psycopg2 but works via command line

When I try to connect to my postgres server via psycopg2 with a bit of code like this (running python as "username"):

psycopg2.connect(database="apis_master")

I get an error

psycopg2.OperationalError: FATAL:  Ident authentication failed for user "username"

But when I run psql straight from the command line (as user "username") like so:

psql -d apis_master

I connect with no problem.

I can't see what is different between these two connection methods. Am I missing some configuration option with psycopg2?

like image 957
magneticMonster Avatar asked Dec 18 '25 20:12

magneticMonster


1 Answers

Your command line user and your python user are different. When you instantiate psycopg2 object, pass user credentials:

db = psycopg2.connect(
                     host = 192.168.0.1, # example IP, use 'localhost' if local, otherwise the IP of the server where Postgre is located.
                     database = 'apis_master'
                     user = 'my_db_user',
                     password = 'my_db_password'
                     )

This should solve your connection problem.

like image 134
That1Guy Avatar answered Dec 21 '25 10:12

That1Guy