Rainette
Rainette

Reputation: 23

Django, psycopg2 and database not found

I recently (yesterday) decided to try Django. So i followed this guide

everything seems to work fine, until i tried this command :

python manage.py syncdb

Yes i know, syncdb is depeciated so i used migrate. and then here is what i get :

(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

here is what i understand : psycopg2 doesnt find my database (oh, really?). I can assure you it exists :

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)

Here is my 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':'',
    }
}

(i've just replaced my password with XXX...) (it doesn't look that safe, but this is another problem)

I've searched on the internet some solutions, and i've found this : Postgresql & psycopg2: database does not exist

and this one :

Creating a postgresql DB using psycopg2

But it didn't help me that much. Could you help me to understand what's going on please? Thank you )))

Upvotes: 0

Views: 3347

Answers (2)

jape
jape

Reputation: 2901

You only need to put the name of the database in your settings; not the path:

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

Upvotes: 2

calosh
calosh

Reputation: 89

You should pass the name of the database, not the filename.

    DATABASES = {
    'default': {
        ...
        'NAME': 'demo_db',
        ...
    }
}

Upvotes: 0

Related Questions