Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django settings.py + dj_database_url on Heroku?

Tags:

django

heroku

Im following the getting started with Django on Heroku - and it shows to set up dj_database_url...

DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}

Am I supposed to leave this exactly as above? thats what I did - and my site appears to work just fine...

OR

Am I supposed to swap out something in the above with DATABASE_URL - which gets pulled from the heroku config set ups?

With not using DATABASE_URL anywhere -- it appears to be working on heroku with the above set up and no change.... but when I look at the DB with a pg:info - I get 0 connections... which makes me think there is no connection to the postgresql -- but hows it working then??

I don't quite understand yet how dj_database_url is working on Heroku with this...can anyone shed a bit more light on this?

heroku pg:info
=== HEROKU_POSTGRESQL_GOLD_URL (DATABASE_URL)
Plan:        Dev
Status:      available
Connections: 0
PG Version:  9.1.6
Created:     2012-10-07 16:11 UTC
Data Size:   6.6 MB
Tables:      12
Rows:        27/10000 (In compliance)
Fork/Follow: Unavailable
like image 684
tbarbe Avatar asked Jan 15 '23 09:01

tbarbe


1 Answers

It is correct as-is.

When running on Heroku, there is an environment variable set (DATABASE_URL) which contains the database URL (a string like postgres://, but with a long autogenerated username/password/database-name, and the host is on amazonaws usually)

When running locally, DATABASE_URL is not set, so your default = '...' database URL is used instead (allows you to run the code locally for development, and deploy to Heroku, without changing any code).

This is based on the "12factor methodology" (the whole document pretty much describes how Heroku is structured)

The dj_database_url.config just parses the username/password/host/db-name from the URL, and splits it into the dictionary format expected by Django - the code is rather simple, if you are curious

like image 159
dbr Avatar answered Jan 26 '23 00:01

dbr