Does psycopg2.connect inherit the proxy set in this context manager?

Viewed 392

I have a Django app below that uses a proxy to connect to an external Postgres database. I had to replace another package with psycopg2 and it works fine locally, but doesn't work when I move onto our production server which is a Heroku app using QuotaguardStatic for proxy purposes. I'm not sure what's wrong here

For some reason, the psycopg2.connect part returns an error with a different IP address. Is it not inheriting the proxy set in the context manager? What would be

from apps.proxy.socks import Socks5Proxy
import requests

PROXY_URL = os.environ['QUOTAGUARDSTATIC_URL']


with Socks5Proxy(url=PROXY_URL) as p:
    public_ip = requests.get("http://wtfismyip.com/text").text
    print(public_ip) # prints the expected IP address
    print('end')
        try:
            connection = psycopg2.connect(user=EXTERNAL_DB_USERNAME,
                                              password=EXTERNAL_DB_PASSWORD,
                                              host=EXTERNAL_DB_HOSTNAME,
                                              port=EXTERNAL_DB_PORT,
                                              database=EXTERNAL_DB_DATABASE,
                                              cursor_factory=RealDictCursor  # To access query results like a dictionary
                                              )  # , ssl_context=True
        except psycopg2.DatabaseError as e:
                logger.error('Unable to connect to Illuminate database')
                raise e
        

Error is:

    psycopg2.OperationalError: FATAL:  no pg_hba.conf entry for host "12.345.678.910", user "username", database "databasename", SSL on

Basically, the IP address 12.345.678.910 does not match what was printed at the beginning of the context manager where the proxy is set. Do I need to set a proxy another method so that the psycopg2 connection uses it?

0 Answers
Related