Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 369
John Lucas Avatar asked Jun 11 '13 04:06

John Lucas


1 Answers

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.

like image 94
David S Avatar answered Oct 03 '22 01:10

David S