I'm working on my first-ever Heroku/Django app. I just want to be sure I'm setting my DATABASE_URL
and DATABASES
variables correctly. 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
. I set the DATABASE_URL
since doing so appears to solve this issue (after I create the my_app
database).
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. 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? Or will this not happen at all? Am I just too confused/tired at this point?
"database url" in default string have the value postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...] Show activity on this post. Use pip module dj-dtabase-url. Add an environmental var with the name 'DATABASE_URL' and the value from the heroku db settings.
The dj_database_url. config method returns a Django database connection dictionary, populated with all the data specified in your URL. There is also a conn_max_age argument to easily enable Django's connection pool.
DATABASE_URL contains the URL your app uses to access the database. If your app already has a Heroku Postgres database and you've provisioned another one, this config var's name instead has the format HEROKU_POSTGRESQL_<COLOR>_URL (for example, HEROKU_POSTGRESQL_YELLOW_URL ).
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'
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With