6

I'm here because I'm really really new with heroku-python-django-postgresql group. I have googled for a usage for dj-database-url and I don't understand why i have to use it when developing a python application that needs to connect with postgresql. I have added postgresql (dev version) as add-on to my application, but I don't know how to tell to the app that I want it to use my db.

so, the short question is, How do I indicate to dj-database-url that I want to use my database?

Thanks for your time and answers, I'll appreciate your help because this is very very urgent!

3 Answers 3

13

dj-database-url is a utility to help you load your database into your dictionary from the DATABASE_URL environment variable. Heroku uses environment variables for your database and other addons. To begin using your database you'd simply use the below command to setup your DATABASES dictionary:

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

And maybe stash DATABASE_URL in your virtualenv activate script.

Sign up to request clarification or add additional context in comments.

5 Comments

How does one go about setting up their local development environment, so that the settings that heroku requires will work in the local environment?
If you run export DATABASE_URL=yourlocaloryourremotedburl then it should work the same as Heroku. Additionally if you use foreman which installs with the heroku toolbelt it will automatically export the values in a .env file.
Just a note. default keyword argument is optional. If you don't specify it, it looks for DATABASE_URL env variable by default.
Using this results in a build failure with the error 'NameError: name 'DATABASES' is not defined.'
just add something like DATABASES={} before it, that'll remove the "not defined" error.
1

May be help for param in setting.py

DATABASES = {
    "default": dj_database_url.config(
        default="postgres://postgres:saleor@localhost:5432/saleor", conn_max_age=600
    )
}

"database url" in default string have the value postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]

Comments

-1

Use pip module dj-dtabase-url. Add an environmental var with the name 'DATABASE_URL' and the value from the heroku db settings.

Important, to avoid the error 'NameError: name 'DATABASES' is not defined.' You still have to leave for example the default DATABSE settings.

The complete code:

import dj_database_url

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
    }
}
DATABASES['default'] = dj_database_url.config()

1 Comment

Unnecessary duplication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.