Django 1.3.1 Heroku Postgres错误

时间:2012-10-25 08:41:35

标签: django postgresql heroku django-settings

我们最近将我们的网站推向了舞台,从那时起就一直在努力解决这个问题,而Heroku的团队并没有及时做出回应,所以我转向社区,看看是否有快速解决方案。 / p>

我们废弃了旧的堆栈并设置了一个仍有相同问题的新堆栈

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'
    },
}

这是完整的痕迹。

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?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

确保你的heroku上有Postgres:

heroku addons:add heroku-postgresql:dev

找出数据库url env变量。它看起来像这样:HEROKU_POSTGRESQL__URL

heroku config | grep POSTGRESQL

更新您的设置

import dj_database_url
import os 

POSTGRES_URL = "HEROKU_POSTGRESQL_<COLOR>_URL"
DATABASES = {'default': dj_database_url.config(default=os.environ[POSTGRES_URL])}
鲍勃是你的叔叔。

我在heroku上为django放了一个方便的引导程序。它可能会有所帮助:https://github.com/callmephilip/django-heroku-bootstrap

快乐部署

答案 2 :(得分:0)

试试这个:

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