简体   繁体   English

我应该如何设置我的 DATABASE_URL?

[英]How should I set my DATABASE_URL?

I'm working on my first-ever Heroku/Django app.我正在开发我的第一个 Heroku/Django 应用程序。 I just want to be sure I'm setting my DATABASE_URL and DATABASES variables correctly.我只是想确保我正确设置了DATABASE_URLDATABASES变量。 Here's what's in my code:这是我的代码中的内容:

import dj_database_url

DATABASE_URL = 'postgresql:///my_app'

# Parse database configuration from $DATABASE_URL
DATABASES = {
    'default': dj_database_url.config(default=DATABASE_URL)
}

When I just have DATABASES['default'] = dj_database_url.config() and I try to use Django commands like run server or migrate I get the following error: NameError: name 'DATABASES' is not defined .当我只有DATABASES['default'] = dj_database_url.config()并且我尝试使用像run servermigrate这样的 Django 命令时,我收到以下错误: NameError: name 'DATABASES' is not defined I set the DATABASE_URL since doing so appears to solve this issue (after I create the my_app database).我设置了DATABASE_URL因为这样做似乎解决了这个问题(在我创建my_app数据库之后)。

Everything appears to be working fine as I code and test, but I've also seen a half-dozen different ways to set the database variables on the internet.在我编写代码和测试时,一切似乎都运行良好,但我还看到了六种不同的方法来在 Internet 上设置数据库变量。 If this isn't correct, I'd like to fix it now.如果这不正确,我想现在修复它。 The thing that really confuses me is, when I push my app to Heroku, how will the data get pushed to the web, when the database is /usr/local/var/postgres?真正让我困惑的是,当我将我的应用程序推送到 Heroku 时,当数据库是 /usr/local/var/postgres 时,数据将如何推送到网络? Or will this not happen at all?或者这根本不会发生? Am I just too confused/tired at this point?在这一点上我是不是太困惑/累了?

This is documented on Heroku Devecenter这是在Heroku Devcenter记录的

# Parse database configuration from $DATABASE_URL
import dj_database_url
# DATABASES['default'] =  dj_database_url.config()
#updated
DATABASES = {'default': dj_database_url.config(default='postgres://user:pass@localhost/dbname')}

If you need Database connection pooling add this bits too.如果您需要数据库连接池,也添加这些位。 More details 更多细节

# Enable Connection Pooling
DATABASES['default']['ENGINE'] = 'django_postgrespool'

This is a simple matter of logic.这是一个简单的逻辑问题。 You can't set the "default" key of the DATABASES dictionary before you have defined the dictionary itself.在定义字典本身之前,您不能设置 DATABASES 字典的“默认”键。

Whether or not you set the default parameter to dj_database_url inside the call or as a separate DATABASE_URL variable is irrelevant, especially as that won't even be used on Heroku as it will be overridden by environment variables.您是否在调用中将default参数设置为dj_database_url或作为单独的DATABASE_URL变量是无关紧要的,尤其是因为它甚至不会在 Heroku 上使用,因为它会被环境变量覆盖。

This allows you to use any database settings during development, but at production (on Heroku), DATABASES['default'].update(db_from_env) changes the database settings to the one created by Heroku.这允许您在开发期间使用任何数据库设置,但在生产中(在 Heroku 上), DATABASES['default'].update(db_from_env)将数据库设置更改为由 Heroku 创建的设置。

import dj_database_url


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


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

You could use any database settings, but the last two lines allow heroku to create its own database for you.您可以使用任何数据库设置,但最后两行允许 heroku 为您创建自己的数据库。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM