Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Django to Heroku (Psycopg2 Error)

So I'm following the getting started guide from heroku with django. However when I run this command:

heroku run python manage.py syncdb

I get this error

psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?

I assumed this meant that the db wasn't set up yet... so I manually added the shared_db option as well:

heroku addons:add shared-database:5mb

But.. I still get the same error. What gives?

like image 236
Msencenb Avatar asked May 15 '12 08:05

Msencenb


People also ask

What is Django_heroku?

Project description This is a Django library for Heroku applications that ensures a seamless deployment and development experience.


1 Answers

EDITED:

As @mipadi has pointed out here (http://stackoverflow.com/questions/13001031/django-heroku-settings-injection/13092534), it can actually be as simple as this:

import dj_database_url

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

This works if you have a DATABASE_URL env variable set. heroku:pg_promote gets your there. Details below


Make sure you have Postgres on your Heroku

heroku addons:add heroku-postgresql:dev

Step 1: figure out your database url

heroku config | grep POSTGRESQL

The output will look something like this:

HEROKU_POSTGRESQL__URL: postgres://user:password@host:5432/blabla

Step 2: Grab the setting name from the previous step (e.g. HEROKU_POSTGRESQL_ROSE_URL) and put it in your settings file like so

DATABASES = {'default': dj_database_url.config(default=os.environ["HEROKU_POSTGRESQL_ROSE_URL"])}

[UPDATE] As Ted has pointed out, there's a way to promote the color URL to DATABASE_URL variable:

heroku pg:promote HEROKU_POSTGRESQL_ROSE_URL

Your database settings can then use DATABASE_URL as opposed to more exotic colored URLS

DATABASES = {'default': dj_database_url.config(default=os.environ["DATABASE_URL"])}

Bob is your uncle

like image 147
Philip Nuzhnyy Avatar answered Sep 24 '22 12:09

Philip Nuzhnyy