【问题标题】:Changing from Sqllite to Postgres on Heroku- why it is not working?在 Heroku 上从 Sqlite 更改为 Postgres - 为什么它不起作用?
【发布时间】:2017-08-19 11:01:05
【问题描述】:

我有点困惑,希望有人能帮助我

最初我使用 sqllite db 将我的 django 应用程序上传到 heroku 帐户

这是我在数据库设置文件中的内容

 DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.sqlite3',
          'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
      }  }

import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

由于 SQLlite 每 24 小时刷新一次到其原始状态,所以效果不佳(但由于我需要演示系统,所以效果很好)

但是现在我必须让它高效,所以我想更改数据库以连接到 postgres 。所以我使用了我用 heroku 创建的 postgres DB 的凭据,我的设置中的 db 看起来像这样

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'd4drq1yytest',
        'USER': 'xvvqgkjtest',
        'PASSWORD': 'test5y55y5y5y5y5y5y5y5y5y54y45',
        'HOST': '777-77-77-67-7.compute-1.amazonaws.com',
        'PORT': '5432',
    }
}

import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

但即使我更改了设置文件并重新部署了新文件,我仍然看到我的 heroku 应用程序已连接到我的 sqllite 数据库。

我做错了什么? (我使用 github 上的 master brunch 进行部署。)

【问题讨论】:

    标签: django postgresql heroku


    【解决方案1】:

    如果您还没有,您可能需要将 heroku postgres 作为应用添加到您的 heroku 应用的仪表板中。

    看来您使用的代码与我用于将 heroku 的 postgres 连接到 heroku 的代码相同。你应该只需要声明

    import dj_database_url db_from_env = dj_database_url.config() DATABASES['default'].update(db_from_env)

    然后确保您已安装 pip whitenoise 并将其添加到 requirements.txt。提交并推送到 heroku 后,您可能希望在 cmd 或终端上运行 heroku run bash。然后你可能想在之后运行python manage.py makemigrations yourappname,你可能想运行python manage.py migrate,如果你没有收到错误,你可能想检查你的应用程序的heroku仪表板,点击postgres,看看是否有任何postgres 的表正在使用中。如果是,那么您正在使用 heroku postgres。

    heroku 可能不需要下面的代码行

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'd4drq1yytest', 'USER': 'xvvqgkjtest', 'PASSWORD': 'test5y55y5y5y5y5y5y5y5y5y54y45', 'HOST': '777-77-77-67-7.compute-1.amazonaws.com', 'PORT': '5432', } }

    这应该是您调用要使用本地主机测试的数据库的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-27
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2017-11-28
      相关资源
      最近更新 更多