【问题标题】:Django-Heroku-PostGres: KeyError: 'psql' during Git push Heroku mainDjango-Heroku-PostGres: KeyError: 'psql' during Git push Heroku main
【发布时间】:2021-07-21 05:01:59
【问题描述】:

我正在尝试在 Heroku 中部署我的 Django 应用程序。我也在使用 PostGreSQL 和 Github。我一直在使用一堆随机教程来设置它并尝试调试结果,所以如果有“不一致”,我深表歉意。

当我运行git push heroku main 时,我收到错误remote: KeyError: 'psql' Error while running '$ python manage.py collectstatic --noinput

这是回溯。

remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "/tmp/build_e87847fa/manage.py", line 22, in <module>
remote:            main()
remote:          File "/tmp/build_e87847fa/manage.py", line 18, in main
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line  
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 363, in execute
remote:            settings.INSTALLED_APPS
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
remote:            self._setup(name)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
remote:            self._wrapped = Settings(settings_module)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
remote:            mod = importlib.import_module(self.SETTINGS_MODULE)
remote:          File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
remote:          File "<frozen importlib._bootstrap_external>", line 790, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
remote:          File "/tmp/build_e87847fa/medrec_project/settings.py", line 106, in <module>
remote:            DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/dj_database_url.py", line 55, in config
remote:            config = parse(s, engine, conn_max_age, ssl_require)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/dj_database_url.py", line 103, in parse
remote:            engine = SCHEMES[url.scheme] if engine is None else engine
remote:        KeyError: 'psql'
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.

这是我认为与我的设置文件相关的内容:

import os
import django_heroku
import dj_database_url
import psycopg2

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD':os.getenv('DB_PASS'),
        'HOST': 'localhost',
        'PORT':'5432',
    }
}
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)

django_heroku.settings(locals())

我的代码中唯一有psql 的地方是我的.env 文件:

SECRET_KEY='<mysecretkey>'
DATABASE_URL='psql://urser:un-githubbedpassword@127.0.0.1:8458/database'
SQLITE_URL='sqlite:///my-local-sqlite.db'
CACHE_URL='memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213'
REDIS_URL='rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=ungithubbed-secret'
DB_NAME='medrecapp'
DB_USER='<myusername>'
DB_PASS='<mypassword>'
DB_HOST='127.0.0.1'

当我运行 py manage.py collectstatic 时,一切运行良好。

据我所知,我已将 PostGreSQL 添加到 PATH(指向 PostGreSql 的 bin 文件夹)。

我可以在终端中使用psql 没有任何问题

我可以毫无问题地将更改推送到 git。

我已使用 .env 文件中的所有内容更新了我的 Heroku 设置 Config Vars

有人知道怎么解决吗?

谢谢!

【问题讨论】:

    标签: django postgresql github heroku


    【解决方案1】:

    解决方法是将.env中postgres的地址从'psql://...'改为'pgsql://...'

    在你的情况下会是这样的

    DATABASE_URL='pgsql://urser:un-githubbedpassword@127.0.0.1:8458/database'

    知道为什么会发生这种情况并不是必须的,但如果您有兴趣...这是因为 dj_database_url 有时会根据 DATABASE_URL 的第一个单词选择数据库的引擎。

    检查我们可以看到的 SCHEMAS 数组

    {'postgres': 'django.db.backends.postgresql_psycopg2', 'postgresql': 'django.db.backends.postgresql_psycopg2', 'pgsql': 'django.db.backends.postgresql_psycopg2', 'postgis': 'django.contrib.gis.db.backends.postgis', 'mysql': 'django.db.backends.mysql', 'mysql2': 'django.db.backends.mysql', 'mysqlgis': 'django.contrib.gis.db.backends.mysql', 'mysql-connector': 'mysql.connector.django', 'mssql': 'sql_server.pyodbc', 'spatialite': 'django.contrib.gis.db.backends.spatialite', 'sqlite': 'django.db.backends.sqlite3', 'oracle': 'django.db.backends.oracle', 'oraclegis': 'django.contrib.gis.db.backends.oracle', 'redshift': 'django_redshift_backend'}

    他们有“postgres”、“pgsql”和“postgresql”的条目,但没有“psql”的条目,这就是我们得到“KeyError”错误的原因。

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 1970-01-01
      • 2013-06-11
      • 2021-05-19
      • 2021-03-19
      • 2022-11-24
      • 2013-03-10
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多