1

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

Max AweTery
  • 103
  • 6

2 Answers2

0

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.

Adrian Klaver
  • 15,886
  • 2
  • 17
  • 28
  • I don't know if you get it or not, but I 've changed the real names of all the variables that are presented here. The real name of the server is like "letters_01". – Max AweTery Sep 03 '20 at 07:24
  • The answer still holds, whatever the name is it needs to be reachable from where you are making the connection. So can you reach that server from the machine you are connection from with some other program? – Adrian Klaver Sep 03 '20 at 13:53
0

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.

Max AweTery
  • 103
  • 6