Home > OS >  OperationalError: (psycopg2.OperationalError) could not translate host name "143@postgres"
OperationalError: (psycopg2.OperationalError) could not translate host name "143@postgres"

Time:04-29

I want to import a large csv file without creating table before for that csv file into postgresql. While searching on stackoverflow, one of the user suggested the following code:

from sqlalchemy import create_engine
engine = create_engine('postgresql://username:password@localhost:5432/mydatabase')
df.to_sql('table_name', engine)

Based on this I wrote the following code based on the information for my postgresql which has username = "postgres" and password = "Katherine@412":

from sqlalchemy import create_engine
engine = create_engine('postgresql://[postgres]:Katherine@412@postgres:5432/covid_deaths.csv')
df.to_sql('covid_deaths_owid', engine)

However it is giving me the following error:

OperationalError: (psycopg2.OperationalError) could not translate host name "412@postgres" to address: Unknown server error

Can someone tell me what I am doing wrong?

CodePudding user response:

localhost ist a valid hostname, like 127.0.0.1 or any other ip adresses.

As long as your maschine or you have an actual server that you can ping with the name postgres.

So determine where your postgres server is running, if the local machine localhostis the correct server name, if ot is another machine, you need to enter theioadrees or the name you can reach it

more about hostgnames you can find in wikipedia

CodePudding user response:

Looks like it's deciding that the host name comes after the first ampersand that it finds. Can you change the password so that it doesn't have an ampersand in it?

  • Related