使用Psycopg2和Python的端口的DB连接出错

问题描述 投票:0回答:1

对开发很新 - 我在部署.py脚本时看到以下错误。我引用了我的信条如下:

参考:

# Postgresql initialization
connection = psycopg2.connect(
    host= "Host",
    dbname= "Database",
    port= "Port",
    user= "User",
    password= "Password")

错误消息:

error:
    2018-09-16T18:19:45.000000+00:00 app[api]: Build succeeded
    2018-09-16T18:19:46.501301+00:00 heroku[web.1]: Process exited with status 1
    2018-09-16T18:19:46.521042+00:00 heroku[web.1]: State changed from starting to crashed
    2018-09-16T18:19:46.408361+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
    2018-09-16T18:19:46.408378+00:00 app[web.1]:   """)
    2018-09-16T18:19:46.409434+00:00 app[web.1]: Traceback (most recent call last):
    2018-09-16T18:19:46.409439+00:00 app[web.1]:   File "app.py", line 24, in <module>
    2018-09-16T18:19:46.409629+00:00 app[web.1]:     password= "Password")
    2018-09-16T18:19:46.409631+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    2018-09-16T18:19:46.409824+00:00 app[web.1]:     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    2018-09-16T18:19:46.409851+00:00 app[web.1]: psycopg2.OperationalError: invalid port number: "Port"
    2018-09-16T18:19:46.409855+00:00 app[web.1]:

有人可以帮忙吗?为什么我的端口抛出问题?

python-2.7 port psycopg2 heroku-postgres
1个回答
0
投票

在提供的代码中,您传递的字符串与建立连接无关。相反,您应该从环境中读取URL,密码,端口等。实际上,Heroku代表您将所有这些组合成一个连接字符串。试试这个(heroku documentation):

DATABASE_URL = os.environ['DATABASE_URL']

conn = psycopg2.connect(DATABASE_URL, sslmode='require')
© www.soinside.com 2019 - 2024. All rights reserved.