为什么psycopg2在将Django站点部署到Heroku时引发PostgreSQL错误?

dly7yett  于 2023-05-07  发布在  Go
关注(0)|答案(1)|浏览(121)

我试图将我的Django站点部署到heroku,并得到以下错误代码段:

2023-05-05T08:42:38.330672+00:00 app[web.1]: from psycopg2._psycopg import (                     # noqa
2023-05-05T08:42:38.330672+00:00 app[web.1]: SystemError: initialization of _psycopg raised unreported exception
2023-05-05T08:42:38.330718+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [8] [INFO] Worker exiting (pid: 8)
2023-05-05T08:42:38.367087+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [2] [WARNING] Worker with pid 8 was terminated due to signal 15
2023-05-05T08:42:38.465035+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [2] [INFO] Shutting down: Master
2023-05-05T08:42:38.465071+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [2] [INFO] Reason: Worker failed to boot.
2023-05-05T08:42:38.616316+00:00 heroku[web.1]: Process exited with status 3
2023-05-05T08:42:38.666808+00:00 heroku[web.1]: State changed from starting to crashed
2023-05-05T08:42:39.000000+00:00 app[api]: Build succeeded
2023-05-05T08:42:44.863150+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=a-coders-journey.herokuapp.com request_id=d8e2f6b2-c4f5-461f-8914-a8e84dc13551 fwd="93.204.33.78" dyno= connect= service= status=503 bytes= protocol=https

我已确保我的requirements.txt文件的版本具有二进制版本(psycopg 2-binary),以及所有适用的要求:

#
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
#    pip-compile --output-file=requirements.txt requirements.in
#
asgiref==3.3.4
    # via
    #   -r requirements.in
    #   django
certifi==2022.12.7
    # via
    #   cloudinary
    #   requests
cffi==1.15.1
    # via cryptography
charset-normalizer==3.1.0
    # via requests
cloudinary==1.25.0
    # via
    #   -r requirements.in
    #   dj3-cloudinary-storage
cryptography==3.4.8
    # via
    #   -r requirements.in
    #   pyjwt
defusedxml==0.7.1
    # via python3-openid
dj-database-url==0.5.0
    # via -r requirements.in
dj3-cloudinary-storage==0.0.5
    # via -r requirements.in
django==3.2.3
    # via
    #   -r requirements.in
    #   django-allauth
    #   django-summernote
django-allauth==0.44.0
    # via -r requirements.in
django-crispy-forms==1.11.2
    # via -r requirements.in
django-summernote==0.8.11.6
    # via -r requirements.in
gunicorn==20.1.0
    # via -r requirements.in
idna==3.4
    # via requests
oauthlib==3.1.1
    # via
    #   -r requirements.in
    #   requests-oauthlib
pillow==9.5.0
    # via -r requirements.in
psycopg2-binary
    # via -r requirements.in
pycparser==2.21
    # via cffi
pyjwt[crypto]==2.1.0
    # via
    #   -r requirements.in
    #   django-allauth
python3-openid==3.2.0
    # via
    #   -r requirements.in
    #   django-allauth
pytz==2021.1
    # via
    #   -r requirements.in
    #   django
requests==2.30.0
    # via
    #   dj3-cloudinary-storage
    #   django-allauth
    #   requests-oauthlib
requests-oauthlib==1.3.0
    # via
    #   -r requirements.in
    #   django-allauth
six==1.16.0
    # via cloudinary
sqlparse==0.4.1
    # via
    #   -r requirements.in
    #   django
urllib3==2.0.2
    # via
    #   cloudinary
    #   requests

# The following packages are considered to be unsafe in a requirements file:
# setuptools

安装。我打开了一个支持票与heroku和他们给了我链接:https://devcenter.heroku.com/articles/connecting-heroku-postgres#connecting-in-python,我也跟着做了,但没有成功。我的settings.py文件,与进口等相关:

from pathlib import Path
import os
import psycopg2
import dj_database_url
if os.path.isfile('env.py'):
    import env

#,,,other code then:

DATABASE_URL = os.environ['DATABASE_URL']

conn = psycopg2.connect(DATABASE_URL, sslmode='require')

then at the very bottom of the settings.py:

DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)

我在Django中的Python版本:简体中文
Heroku Python版本:Python 3.11.3
我做错了什么?为什么psycopg 2在初始化时出错?先谢谢你了。
我从psycopg 2的旧版本转到了二进制版本。我试过Heroku的支持文档演练
我试着安装一个runtime.txt文件,并强制heroku应用程序使用与我的Django版本相同的版本,我已经确保我的www.example.com文件的内容env.py正确指向我正在使用的elephantSQL数据库,这两个变量也在我的Heroku配置变量中。

xdnvmnnf

xdnvmnnf1#

1.忽略连接到Python部分,它与您无关,对于Django应用程序,您只需要'连接Django'部分。

  1. Django部分应该如下
# Define database URL from the environment file
DATABASE_URL = os.environ['DATABASE_URL']

# Configure default database
DATABASES = {
    'default': dj_database_url.config(
        conn_max_age=600,
        conn_health_checks=True,
        ssl_require=True,
    ),
}

相关问题