找不到 Django、psycopg2 和数据库

Django, psycopg2 and database not found

我最近(昨天)决定尝试 Django。 所以我关注了 this guide

似乎一切正常,直到我尝试了这个命令:

python manage.py syncdb

是的,我知道,syncdb 已贬值,所以我使用了 migrate。然后这是我得到的:

(project)dragmetohell@dmth:/opt/project/newproject$ python manage.py migrate
Traceback (most recent call last):
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/opt/project/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL:  database "/opt/project/newproject/demo_db" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/opt/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/project/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/project/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/opt/project/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 89, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/opt/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/opt/project/lib/python3.5/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/opt/project/lib/python3.5/site-packages/django/db/migrations/loader.py", line 176, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/opt/project/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/opt/project/lib/python3.5/site-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 "/opt/project/lib/python3.5/site-packages/django/db/backends/base/base.py", line 231, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/base/base.py", line 204, in _cursor
    self.ensure_connection()
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/opt/project/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/project/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/project/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/opt/project/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  database "/opt/project/newproject/demo_db" does not exist

这是我的理解: psycopg2 没有找到我的数据库(哦,真的吗?)。我可以向你保证它存在 :

postgres=# \list
                                   List of databases
   Name    |   Owner   | Encoding |   Collate   |    Ctype    |    Access privileges    
-----------+-----------+----------+-------------+-------------+-------------------------
 demo_db   | demo_user | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/demo_user          +
           |           |          |             |             | demo_user=CTc/demo_user
 postgres  | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres            +
           |           |          |             |             | postgres=CTc/postgres
 template1 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres            +
           |           |          |             |             | postgres=CTc/postgres
(4 rows)

这是我的 settings.py :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.path.join(BASE_DIR, 'demo_db'),
    'USER': 'demo_user',
    'PASSWORD': 'XXXXXXXXXX',
    'HOST': 'localhost',
    'PORT':'',
    }
}

(我刚刚用 XXX 替换了我的密码...)(看起来不太安全,但这是另一个问题)

我在互联网上搜索了一些解决方案,我发现了这个: Postgresql & psycopg2: database does not exist

还有这个:

Creating a postgresql DB using psycopg2

但这对我帮助不大。你能帮我理解这是怎么回事吗?谢谢 )))

您应该传递数据库的名称,而不是文件名。 </p> <pre><code> DATABASES = { 'default': { ... 'NAME': 'demo_db', ... } }

您只需要在设置中输入数据库的名称即可;不是路径:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'demo_db',
        'USER': 'demo_user',
        'PASSWORD': 'XXXXXXXXXX',
        'HOST': 'localhost',
        'PORT':'',
    }
}