Django settings.py + dj_database_url on Heroku? - django

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

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

Related

Flask-migrate doesn't update or migrate changes after environment change

I'm running Flask with FLask-SQLalchemy on DigitalOcean Apps. I can't get Flask-Migrate to work properly on the production environment. Calling flask db migrate on my production app does nothing. No changes detected. Nothing. Just this below:
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
As such I tried this tutorial - which is basically a way of 'starting again'. I'd create the initial migration to a local empty DB, then commit that migration script to source and push it live. The changes (the initial migration) were detected just fine on an empty local DB. All that's changed is the DB is empty and I changed the DATABASE_URI env.
This means there's a migration for the 'first migration' on the production instance. As far as I'm aware, it reflects the state of the production DB.
I'd then run flask db stamp head on production and local. Running flask db migrate on production (with the change I want to migrate) nothing happens. No changes detected. flask db upgrade produces the same results. I checked the migrate instance on production - it has the correct db connection string. The web app works, but its not detecting or able to push through new changes?
I have data in my database I absolutely cannot drop it.

force heroku / django to use local database

I have a django based herokuapp site. I set everything up a long time ago and am now unable to get things working with a local instance of postgresql. In my settings file, I updated:
DATABASES['default'] = dj_database_url.config()
to work with a local database:
DATABASES['default'] = dj_database_url.config(default='postgres://localhost/appDB')
When running foreman, I can view the site, but the database is not currently populated (although I did create the empty DB). Running:
heroku run python manage.py dumpdata
Returns the contents of the remote (herokuapp) database, while a syncdb command results in "Installed 0 object(s) from 0 fixture(s)". So it looks like I'm still contacting the remote database. I'm pretty sure the postgresql DB is setup correctly locally; how can I force the app to use it?
I'm sure this is simple, but I haven't seen anything useful yet. I did try
export DATABASE_URL=postgres:///appDB
but that hasn't helped.
Cheers

Making changes to postgres databse on heroku - django app

Hey Im using heroku for the first time and I made some changes to some models in Django.
This is what I do:
heroku pg:info --app podiium-staging
=== HEROKU_POSTGRESQL_TEAL_URL
Plan: Dev
Status: available
Connections: 0
PG Version: 9.1.11
Created: 2012-10-20 00:57 UTC
Data Size: 9.1 MB
Tables: 61
Rows: 48/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Then:
heroku pg:psql HEROKU_POSTGRESQL_TEAL_URL --app=podiium-staging
---> Connecting to HEROKU_POSTGRESQL_TEAL_URL
psql (9.3.3, server 9.1.11)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
podiium-staging::TEAL=> alter table debate_debate add column debate_image text;
Ok So I thaught I had altered the database, but in django that column does not exist.
So I guess Im making changes to another DB?
Please help me, I am so confused with this sh%T
What are your database settings in settings.py?
Also, have you considered using South for schema changes? It makes it much easier.
http://south.readthedocs.org/en/latest/index.html

dj_database_url not properly finding heroku postgres db configurations

I am having a problem getting django to recognize my heroku postgres db. Usually the following configuration work fine, but for some reason it has stopped working on my most recent application.
In settings.py, I added the following code.
import dj_database_url
DATABASES['default'] = dj_database_url.config()
print DATABASES['default']
it prints out {'default':{}}
the heroku pg:info command returns what looks to be normal:
=== HEROKU_POSTGRESQL_WHITE_URL
Plan: Dev
Status: available
Connections: 1
PG Version: 9.2.4
Created: 2013-08-06 15:40 UTC
Data Size: 6.3 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
When I go to use the database it returns
settings.DATABASES is improperly configured.
Any thoughts on why dj_database_url.config() is not properly parsing the database configurations?
Thank you for the thoughts.
EDIT:
Still don't know the reason for the above behavior, but I found a work around. I had to add the postgres db to the app before the initial push. For some reason it wasn't finding it if it was added after the first push.
This should be your db settings
DATABASES['default'] = dj_database_url.config(default='postgres://user:passwprd#host/db')
DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'

Configuring postgresql database for local development in Django while using Heroku

I know there are a lot of questions floating around there relating to similar issues, but I think I have a specific flavor which hasn't been addressed yet. I'm attempting to create my local postgresql database so that I can do local development in addition to pushing to Heroku.
I have found basic answers on how to do this, for example (which I think is a wee bit outdated):
'#DATABASES = {'default': dj_database_url.config(default='postgres://fooname:barpass#localhost/dbname')}'
This solves the "ENGINE" is not configured error. However, when I run 'python manage.py syncdb' I get the following error:
'OperationalError: FATAL: password authentication failed for user "foo"
FATAL: password authentication failed for user "foo"'
This happens for all conceivable combinations of username/pass. So my ubuntu username/pass, my heroku username/pass, etc. Also this happens if I just try to take out the Heroku component and build it locally as if I was using postgresql while following the tutorial. Since I don't have a database yet, what the heck do those username/pass values refer to? Is the problem exactly that, that I need to create a database first? If so how?
As a side note I know I could get the db from heroku using the process outlined here: Should I have my Postgres directory right next to my project folder? If so, how?
But assuming I were to do so, where would the new db live, how would django know how to access it, and would I have the same user/pass problems?
Thanks a bunch.
Assuming you have postgres installed, connect via pgadmin or psql and create a new user. Then create a new database and with your new user as the owner. Make sure you can connect via psql with the new user into to the database. you will then need to set up an env variable in your postactivate file in your virtualenv's bin folder and save it. Here is what I have for the database:
export DATABASE_URL='postgres://{{username}}:{{password}}#localhost:5432/{{database}}'
Just a note: adding this value to your postactivate doesn't do anything. The file is not run upon saving. You will either need to run this at the $ prompt, or simply deactivate and active your virtualenv.
Your settings.py should read from this env var:
DATABASES = {'default': dj_database_url.config()}
You will then configure Heroku with their CLI tool to use your production database when deployed. Something like:
heroku config:set DATABASE_URL={{production value here}}
(if you don't have Heroku's CLI tool installed, you need to do it)
If you need to figure how exactly what that value you need for your production database, you can get it by logging into heroku's postgresql subdomain (at the time this is being written, it's https://postgres.heroku.com/) and selecting the db from the list and looking at the "Connection Settings : URL" value.
This way your same settings.py value will work for both local and production and you keep your usernames/passwords out of version control. They are just env config values.