简体   繁体   English

Heroku + Postgres:psycopg2.OperationalError:无法翻译主机名“无”

[英]Heroku + Postgres: psycopg2.OperationalError: could not translate host name “None”

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"我已经查看了could not translate host name错误的其他一些问题,但我无法找出could not translate host name "None"的解决方案

Context:语境:

  • Recently deployed a Flask app to Heroku最近将 Flask 应用程序部署到 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.根据文档中的说明,我在 Heroku 上配置了一个 postgres db,并将本地 postgres db 的 .dump 推送到 Heroku db。 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.我可以使用heroku pg:psql连接到数据库,当我查询表时,结果如预期。
  • 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.当我尝试使用虚拟用户凭据登录时,出现 500 错误。

Error: When I run heroku logs , the base error seems to be: psycopg2.OperationalError: could not translate host name "None"错误:当我运行heroku logs ,基本错误似乎是: 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.我想可能需要手动设置主机名,但是设置 HOST_NAME 变量时没有得到不同的结果。 I'm having trouble running down the root cause of host name "None"我无法找出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"```


Solved, thanks to @v25's comment.已解决,感谢@v25 的评论。

In my config.py file I incorrectly constructed the postgres URL using environment variables like POSTGRES_USER, POSTGRES_DB, etc. I used to use just DATABASE_URL but changed it for some reason when I migrated from sqlite to postgres.在我的 config.py 文件中,我使用 POSTGRES_USER、POSTGRES_DB 等环境变量错误地构建了 postgres URL。我曾经只使用 DATABASE_URL,但是当我从 sqlite 迁移到 postgres 时,由于某种原因改变了它。

Changing SQLALCHEMY_DATABASE_URI to equal os.environ.get('DATABASE_URL') fixed this issue.SQLALCHEMY_DATABASE_URI更改为等于os.environ.get('DATABASE_URL')修复了此问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 psycopg2.OperationalError:无法使用 psycopg2 和 docker 将主机名转换为地址 - psycopg2.OperationalError: could not translate host name to address using psycopg2 and docker psycopg2.OperationalError:无法将主机名“xxxxxx.us-east-1.rds.amazonaws.com”转换为地址:未知主机 - psycopg2.OperationalError: could not translate host name "xxxxxx.us-east-1.rds.amazonaws.com" to address: Unknown host Docker- django 在连接到 postgres 时抛出错误:psycopg2.OperationalError:无法连接到服务器:连接被拒绝 - Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused psycopg2.OperationalError 错误 - psycopg2.OperationalError errors psycopg2.OperationalError: FATAL: 抱歉,heroku 上已经有太多客户端了 - psycopg2.OperationalError: FATAL: sorry, too many clients already on heroku (psycopg2.OperationalError)无法连接到服务器:连接被拒绝是服务器 - (psycopg2.OperationalError) could not connect to server: Connection refused Is the server 无法使用 Postgres、Docker Compose 和 Psycopg2 将主机名“db”转换为地址 - Could not translate host name "db" to address using Postgres, Docker Compose and Psycopg2 psycopg2.OperationalError:无法创建套接字:打开的文件太多 - psycopg2.OperationalError: could not create socket: Too many open files (psycopg2.OperationalError) 无效 - 操作码 - (psycopg2.OperationalError) Invalid - opcode 数据库不存在(psycopg2.OperationalError) - DATABASE NOT EXIST (psycopg2.OperationalError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM