title: Django, psycopg2 and database not found
body: <p>I recently (yesterday) decided to try Django.
  So i followed <a href="https://vexxhost.com/resources/tutorials/how-to-deploy-django-on-nginx-gunicorn-with-postgres/" rel="nofollow noreferrer">this guide</a> </p>
  
  <p>everything seems to work fine, until i tried this command :</p>
  
  <pre><code>python manage.py syncdb
  </code></pre>
  
  <p>Yes i know, syncdb is depeciated so i used migrate. and then here is what i get :</p>
  
  <pre><code>(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 &lt;module&gt;
      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
  </code></pre>
  
  <p>here is what i understand :
  psycopg2 doesnt find my database (oh, really?). I can assure you it exists :</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Here is my settings.py :</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': os.path.join(BASE_DIR, 'demo_db'),
      'USER': 'demo_user',
      'PASSWORD': 'XXXXXXXXXX',
      'HOST': 'localhost',
      'PORT':'',
      }
  }
  </code></pre>
  
  <p>(i've just replaced my password with XXX...) (it doesn't look that safe, but this is another problem)</p>
  
  <p>I've searched on the internet some solutions, and i've found this :
  <a href="https://stackoverflow.com/questions/31055166/postgresql-psycopg2-database-does-not-exist">Postgresql &amp; psycopg2: database does not exist</a></p>
  
  <p>and this one :</p>
  
  <p><a href="https://stackoverflow.com/questions/19426448/creating-a-postgresql-db-using-psycopg2">Creating a postgresql DB using psycopg2</a></p>
  
  <p>But it didn't help me that much. Could you help me to understand what's going on please? Thank you )))</p>