如何解决Heroku + Postgres:psycopg2.OperationalError:无法转换主机名“ None”
我已经检查了其他一些与could not translate host name
错误有关的问题,但无法为could not translate host name "None"
找到解决方案
上下文:
- 最近将一个Flask应用程序部署到Heroku
- 我按照文档中的说明在Heroku上配置了一个postgres数据库,并将本地postgres数据库的.dump推送到了Heroku数据库。有问题的数据库包含一个具有虚拟用户和登录名的表。我正在使用密码哈希
- 我可以使用
heroku pg:psql
连接到数据库,当我查询表时,结果与预期的一样。 - 这是我第一次部署具有数据库和用户凭据的应用程序。
问题: 该应用程序似乎已正确部署。尝试使用虚拟用户凭据登录时,出现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",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",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",**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')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。