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

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

我已经查看了 could not translate host name 错误的其他一些问题,但无法找到 could not translate host name "None"

的解决方案

上下文:

问题: 该应用程序似乎部署良好。当我尝试使用虚拟用户凭据登录时,出现 500 错误。

错误: 当我 运行 heroku logs 时,基本错误似乎是: psycopg2.OperationalError: could not translate host name "None"

我以为可能需要手动设置主机名,但是当我设置 HOST_NAME 变量时并没有得到不同的结果。我无法 运行找出 host name "None"

的根本原因

这是我的配置:

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

完整错误:

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"```


已解决,感谢@v25 的评论。

在我的 config.py 文件中,我使用 POSTGRES_USER、POSTGRES_DB 等环境变量错误地构建了 postgres URL。我过去只使用 DATABASE_URL 但是当我从 sqlite 迁移到 postgres 时出于某种原因更改了它。

SQLALCHEMY_DATABASE_URI 更改为等于 os.environ.get('DATABASE_URL') 解决了这个问题。

在config.py中: SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')