Connecting to PostgreSQL - DBHOST issue

Hello,

This is my first post and I am having an issue connecting to PostgreSQL.

The error is a psycopg2.OperationalError on line 94 of main.py. It seems that the connection parameters for the PostgreSQL database are not properly set. Specifically, the DB_HOST parameter is not set, resulting in the error message “could not translate host name “None” to address: Name or service not known.”

To fix this, you need to make sure that the DB_HOST environment variable is properly set. Double-check that the value assigned to DB_HOST is correct.

I don’t know how to fix this.

Thanks,

Anthony

In main.py

import psycopg2

DB_URL = os.getenv('DATABASE_URL')
DB_NAME = os.getenv('PGDATABASE')
DB_HOST = os.getenv('PGHOST')
DB_PORT = os.getenv('PGPORT')
DB_USER = os.getenv('PGUSER')
DB_PASSWORD = os.getenv('PGPASSWORD')
conn = psycopg2.connect(
    dbname=DB_NAME,
    user=DB_USER,
    password=DB_PASSWORD,
    host=DB_HOST,
    port=DB_PORT
)

Hi Anthony!

It seems like your environment variable may not be set. Can you ensure your environment variable is set to its proper value by running echo "$DB_HOST"? If nothing is outputted, your environment variable may not exist which can be fixed by running export DB_HOST=yourValue.

1 Like

well, export only applies in the current shell scope. You could consider looking into Secrets (Environment Variables)