致命错误:数据库不存在。

4

我在使用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、密码为collector的用户。同时,我也创建了名为"collector"的模式(schema):

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

我还尝试删除数据库和用户,并重新创建它们。但仍然无法解决问题。可能出了什么问题?我有什么没做对吗?我是个新手,平时使用MySQL。

1个回答

6
在PostgreSQL中,模式并不是数据库。你在名为'postgres'的数据库中创建了一个名为'collector'的模式。但你正在尝试连接到名为'collector'的数据库上。这个数据库不存在,因为你没有创建它。你应该创建一个名为'collector'的数据库,然后只需保留模式(即默认为“public”)。通常情况下,在“postgres”数据库中创建新模式都是一个不好的做法,因为名为“postgres”的数据库通常应该保留用于系统维护任务。

太棒了!我创建了数据库,现在它可以正常工作了。谢谢! - Eric Luther

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接