ApPeL
ApPeL

Reputation: 4911

Django 1.3.1 Heroku Postgres error

We recently pushed our site to staging and have been struggling to get it up ever since, and the team at Heroku are not really responding in time, so I am turning to the community to see if there is a quick fix.

We scrapped the old one and set up a new stack still with the same issues

heroku config
DISABLE_INJECTION: 1

settings.py

import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'PORT': '',
        'HOST': 'localhost'
    },
}

Here is the full trace.

heroku run python myapp/manage.py syncdb

Traceback (most recent call last):
  File "fundedbyme/manage.py", line 11, in <module>
    execute_manager(settings)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/app/.heroku/venv/lib/python2.7/site-packages/south/management/commands/syncdb.py", line 90, in handle_noargs
    syncdb.Command().execute(**options)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 56, in handle_noargs
    cursor = connection.cursor()
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/__init__.py", line 252, in cursor
    cursor = util.CursorWrapper(self._cursor(), self)
  File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 140, in _cursor
    self.connection = Database.connect(**conn_params)
  File "/app/.heroku/venv/lib/python2.7/site-packages/psycopg2/__init__.py", line 179, in connect
    connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" and accepting
    TCP/IP connections on port 5432?

Upvotes: 1

Views: 1679

Answers (3)

Rag Sagar
Rag Sagar

Reputation: 2374

Try this:

import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://myuser:mypassword@localhost:5432/mydb')}

Upvotes: 0

fabiocerqueira
fabiocerqueira

Reputation: 812

DATABASES = {
    'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'))
}

Upvotes: 2

Philip Nuzhnyy
Philip Nuzhnyy

Reputation: 4740

Make sure you have Postgres on your heroku:

heroku addons:add heroku-postgresql:dev

Figure out database url env variable. It's going to look something like this : HEROKU_POSTGRESQL__URL

heroku config | grep POSTGRESQL

Update your settings

import dj_database_url
import os 

POSTGRES_URL = "HEROKU_POSTGRESQL_<COLOR>_URL"
DATABASES = {'default': dj_database_url.config(default=os.environ[POSTGRES_URL])}

Bob is your uncle.

I put together a handy bootstrap for django on heroku. It might be helpful: https://github.com/callmephilip/django-heroku-bootstrap

Happy deploying

Upvotes: 1

Related Questions