24👍
This is documented on Heroku Devecenter
# 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'
4👍
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.
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.
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-Overriding the save method in Django ModelForm
- [Django]-How to pass kwargs from save to post_save signal
2👍
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.
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.
- [Django]-Customize/remove Django select box blank option
- [Django]-Removing 'Sites' from Django admin page
- [Django]-Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given