[Python] Heroku + Postgres: psycopg2.OperationalError: could not translate host name "None"

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    I've reviewed a few other issues with the could not translate host name error, but am unable to figure out a solution for could not translate host name "None"

    Context:

    • Recently deployed a Flask app to Heroku
    • I provisioned a postgres db on Heroku and pushed a .dump of my local postgres db to the Heroku db, per the instructions in the documentation. The db in question contains a table with a dummy user and login. I'm using a password hash
    • I can connect to the db using heroku pg:psql, and when I query the table the results are as expected.
    • This is my first time deploying an application with a database and user credentials.

    Issue: The application seems to have deployed fine. When I attempt to log in with the dummy user credentials, I get a 500 error.

    Error: When I run heroku logs, the base error seems to be: psycopg2.OperationalError: could not translate host name "None"

    I thought maybe the host name needed to be set manually, but I didn't get a different result when I set the HOST_NAME variable. I'm having trouble running down the root cause of host name "None"

    Here is my config:

    AWS_ACCESS_KEY_ID: xyz
    AWS_SECRET_ACCESS_KEY: abx
    DATABASE_URL: postgres://<varchar>.compute-1.amazonaws.com:5432/<varchar>
    FLASK_APP: myapp.py
    HOST_NAME: .herokuapp.com
    LOG_TO_STDOUT: 1
    S3_BUCKET_NAME: myapp


    Full Error:

    self.__connect(first_connect_check=True)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 657, in __connect
    pool.logger.debug("Error on connect(): %s", e)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
    exc_value, with_traceback=exc_tb,
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 652, in __connect
    connection = pool._invoke_creator(self)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 490, in connect
    return self.dbapi.connect(*cargs, **cparams)
    File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    psycopg2.OperationalError: could not translate host name "None" to address: Name or service not known

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
    File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
    File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
    File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File "/app/app/auth/routes.py", line 22, in login
    user = User.query.filter_by(username=form.username.data).first()
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3300, in first
    ret = list(self[0:1])
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3078, in __getitem__
    return list(res)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3405, in __iter__
    return self._execute_and_instances(context)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3427, in _execute_and_instances
    querycontext, self._connection_from_session, close_with_result=True
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3442, in _get_bind_args
    mapper=self._bind_mapper(), clause=querycontext.statement, **kw
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3420, in _connection_from_session
    conn = self.session.connection(**kw)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1133, in connection
    execution_options=execution_options,
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1139, in _connection_for_bind
    engine, execution_options
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 432, in _connection_for_bind
    conn = bind._contextual_connect()
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2251, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2289, in _wrap_pool_connect
    e, dialect, self
    2020-08-23T16:48:45.867504+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1555, in _handle_dbapi_exception_noconnection
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2285, in _wrap_pool_connect
    return fn()
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 773, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
    rec = pool._do_get()
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
    self._dec_overflow()
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
    exc_value, with_traceback=exc_tb,
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
    return self._create_connection()
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
    return _ConnectionRecord(self)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
    self.__connect(first_connect_check=True)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 657, in __connect
    pool.logger.debug("Error on connect(): %s", e)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
    exc_value, with_traceback=exc_tb,
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 652, in __connect
    connection = pool._invoke_creator(self)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
    File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 490, in connect
    return self.dbapi.connect(*cargs, **cparams)
    File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "None" to address: Name or service not known
    (Background on this error at: http://sqlalche.me/e/e3q8)
    at=info method=POST path="/auth/login" host=myapp.herokuapp.com request_id=c1f1df7e-d10d-4e56-b20c-a87788413016 fwd="69.181.181.226" dyno=web.1 connect=1ms service=138ms status=500 bytes=6720 protocol=https
    10.37.196.48 - - [23/Aug/2020:16:48:45 +0000] "POST /auth/login HTTP/1.1" 500 6354 "https://myapp.herokuapp.com/auth/login" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"```

    Continue reading...

Compartilhe esta Página