Max AweTery
Max AweTery

Reputation: 103

Couldn't translate host name PostgreSQL

I'm trying to work with db. So I connect to the PostgreSQL database with the following connection info. I'm using Jupyter Notebook.

from sqlalchemy import create_engine
POSTGRES_DIALECT = 'postgresql'
POSTGRES_SERVER = 'server'
POSTGRES_DBNAME = 'db'
POSTGRES_SCHEMA = 'public' 
POSTGRES_USERNAME = 'user' 
POSTGRES_PASSWORD = 'password'

postgres_str = ('{dialect}://{username}:{password}@{server}/{dbname}'.format(
                    dialect=POSTGRES_DIALECT,
                    server=POSTGRES_SERVER,
                    dbname=POSTGRES_DBNAME,
                    username=POSTGRES_USERNAME,
                    password=POSTGRES_PASSWORD
                ))
# Create the connection
cnx = create_engine(postgres_str)
agreements_df = pd.read_sql_query('''SELECT * from db''', cnx)
agreements_df.head()

There's the error:

OperationalError: (psycopg2.OperationalError) could not translate host name "server" to address: nodename nor servname provided, or not known

enter image description here

Upvotes: 1

Views: 3469

Answers (2)

Max AweTery
Max AweTery

Reputation: 103

I was given the wrong connection information. The problem is in the side that provides the connection info (the port is closed or the error may be related to DNS).

It is better to prescribe ip here, replace "POSTGRES_SERVER = 'server'" with some ip numbers.

Upvotes: 0

Adrian Klaver
Adrian Klaver

Reputation: 19724

It would make more sense to call POSTGRES_SERVER something like POSTGRES_HOST and the argument host instead of server. Then it would be more obvious that 'server' is not a resolvable host name. I would test using psql to see what host name is reachable for the Postgres server you want to connect to.

Upvotes: 0

Related Questions