'OperationalError: (psycopg2.OperationalError) could not translate host name "143@postgres" to address: Unknown server error [duplicate]

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?



Solution 1:[1]

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

Solution 2:[2]

The problem is that the password in your connection string contains the 'at' symbol, @. This is interpreted as marking the end of the password and the beginning of the hostname.

The quick solution is to replace the first @ symbol with %40. The connection string is a kind of URI and %40 is the URI encoding of @ (it will be converted into the correct symbol when the password is processed).

In general you can encode any string in a URI-safe manner as follows, working in Python:

import urllib.parse

encoded = urllib.parse.quote_plus(unencoded)

You also have square brackets around your username for some reason, that may also cause problems after you solve the first problem.

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nbk
Solution 2 Paddy Alton
Solution 3 Paddy Alton