Heroku + Django + Postgres错误:“致命:数据库”python_getting_started“不存在”

时间:2015-06-13 07:27:11

标签: python django postgresql heroku

我正在尝试在Heroku(https://devcenter.heroku.com/articles/getting-started-with-python#introduction)进行Python练习入门。我只是按照说明操作并使用教程中提供的git repo。但是当我转到使用数据库功能的Web应用程序的/ db页面时,我得到了这个错误跟踪:

Environment:


Request Method: GET
Request URL: http://localhost:5000/db

Django Version: 1.8.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'hello')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/hello/views.py" in db
  14.     greeting.save()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/models/base.py" in save
  710.                        force_update=force_update, update_fields=update_fields)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  735.         with transaction.atomic(using=using, savepoint=False):
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/transaction.py" in __enter__
  150.             if not connection.get_autocommit():
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in get_autocommit
  286.         self.ensure_connection()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in ensure_connection
  130.                 self.connect()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  97.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in ensure_connection
  130.                 self.connect()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in connect
  119.         self.connection = self.get_new_connection(conn_params)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py" in get_new_connection
  172.         connection = Database.connect(**conn_params)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/psycopg2/__init__.py" in connect
  164.     conn = _connect(dsn, connection_factory=connection_factory, async=async)

Exception Type: OperationalError at /db
Exception Value: FATAL:  database "python_getting_started" does not exist

崩溃只发生在我本地运行应用程序时; herokuapp.com上的应用程序运行正常。你知道问题可能是什么吗?真的很感激帮助。

1 个答案:

答案 0 :(得分:0)

#try to load local_settings.py if it exists
try:
    from .settings_local import *
except Exception as e:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql_psycopg2",
        }
    }
    import dj_database_url
    DATABASES['default'] = dj_database_url.config()
    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    # Allow all host headers
    ALLOWED_HOSTS = ['']

settings_local.py附近创建settings.py并在此处声明您的数据库设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'python_getting_started',
        'USER': 'some_postgre_user',
        'PASSWORD': 'some_password',
    }
}

如果你没有python_getting_started,你必须创建它:

psql postgres
create database python_getting_started;
create user some_postgre_user password 'some_password';
grant all on database python_getting_started to some_postgre_user;
\q