Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I set my DATABASE_URL?

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?

like image 321
James Kelleher Avatar asked May 05 '15 05:05

James Kelleher


People also ask

How do I use a DJ database URL?

"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.

What is Dj_database_url?

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.

What is DATABASE_URL in Heroku?

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 ).


3 Answers

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'
like image 107
moonstruck Avatar answered Oct 19 '22 19:10

moonstruck


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.

like image 20
Daniel Roseman Avatar answered Oct 19 '22 18:10

Daniel Roseman


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.

like image 38
John Johnson Avatar answered Oct 19 '22 19:10

John Johnson