Search code examples
postgresqldockerubuntuwindows-subsystem-for-linux

I cannot connect to database using WSL Ubuntu, but everything works on windows


I get "Exception has occurred: OperationalError (psycopg2.OperationalError) could not translate host name "stat-hh-postgresql" to address: Name or service not known" when I run a function with Ubuntu but the same function works well with windows.

Also another function that fails to connect to MySQL database via Ubuntu, works well with windows.

def connect_to_modelDB():
        
        engine = create_engine("postgresql+psycopg2://username:password@stat-hh-postgresql:5432/DBname")
        modelDB = engine.connect()
        return modelDB

geos = query_table_from_modelDB("geos")

I get "Exception has occurred: OperationalError (psycopg2.OperationalError) could not translate host name "stat hh postgresql" to address: Name or service not known

(Background on this error at: https://sqlalche.me/e/14/e3q8) psycopg2.OperationalError: could not translate host name "stat-hh-postgresql" to address: Name or service not known"


Solution

  • stat-hh-postgresql cannot be resolved by your ubuntu system's DNS resolver.

    try ping stat-hh-postgresql from ubuntu. Bet that does not work, but works from windows.