【问题标题】:FATAL: database does not exist致命:数据库不存在
【发布时间】:2016-04-30 17:06:35
【问题描述】:

我很难用 postgres 设置 django。

这是我的 settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': 'localhost',
        'NAME': 'collector',
        'USER': 'collector_user',
        'PASSWORD': 'collector'
    }
}

我在 Postgres First steps 网站中创建了带有密码收集器的用户 collector_user。还创建了收集器架构:

postgres=# select nspname from pg_catalog.pg_namespace;
      nspname       
--------------------
 pg_toast
 pg_temp_1
 pg_toast_temp_1
 pg_catalog
 public
 information_schema
 collector
(7 rows)

以下是 django 对此的看法:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 89, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/loader.py", line 176, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 52, in ensure_schema                                                                           
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):                                                                          
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 231, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 204, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  database "collector" does not exist

我还尝试删除数据库和用户并尝试再次创建它。它也没有工作。可能是什么问题?有什么我没有做的吗?我是 postgres 的新手,我通常在 MySQL 上工作。

【问题讨论】:

    标签: postgresql django-models


    【解决方案1】:

    在 PostgreSQL 中,模式不是数据库。您在名为“postgres”的数据库中创建了一个名为“collector”的模式。但是您尝试连接到名为“收集器”的数据库。哪个不存在,因为您没有创建它。您可能应该创建一个名为“collector”的数据库,然后只保留架构(即默认为“public”)。

    无论如何,在“postgres”数据库中创建新架构通常是一种不好的做法,因为名为“postgres”的数据库通常应该保留用于系统维护任务。

    【讨论】:

    • 这改变了一切!我创建了数据库,现在它可以工作了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2013-07-12
    • 2015-07-28
    相关资源
    最近更新 更多