10

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.

2
  • Can you post your entire settings.py? Commented Jun 11, 2013 at 4:06
  • As I remember, for nix OSes you must have for initial at least user named postgres for acess to postgres DB. My experience with PG is limited by Win, Ubunru and CentOS. Try to execute pg_ctl status. Then see if your OS is accepted calls via TCP protocol to Postgres; next, check postgres config pg_hba.conf for acceptable addresses. And so on. Simplest way is to ask Google: "PostgreSQL" [YourOSName] And yes, python no any relations to PG as for other DBMS. Commented Jun 11, 2013 at 4:18

1 Answer 1

18

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.

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

2 Comments

Thanks for the help David. I was able to do the first step following: cyberciti.biz/faq/howto-add-postgresql-user-account. Could you post an example of setting up the env variable, what it should be, and how to make settings.py read from it? I'm a bit confused on that end.
@JohnLucas I have changed my answer to try and address your questions. Please let me know if you need more help.

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.