How to Solve Error Message django.db.utils.OperationalError: FATAL: database “dbname” does not exit

Posted on

Introduction

There is an occurrence of error appear upon running a Django-based internal server. It aims to run a Django application. But unfortunately, there is an error appear. The following is the actual command displaying the appearance of the error message :

(env) C:\programming\python\django>cd myproject
(env) C:\programming\python\django\myproject>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\app\python39\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: database "sysapp" does not exist

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

Traceback (most recent call last):
File "C:\app\python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\app\python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check_migrations()
File "C:\app\python39\lib\site-packages\django\core\management\base.py", line 486, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\app\python39\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\app\python39\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
self.build_graph()
File "C:\app\python39\lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\app\python39\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
if self.has_table():
File "C:\app\python39\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
return self._cursor()
File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
self.ensure_connection()
File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\app\python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\app\python39\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: database "sysapp" does not exit

Actually, the database configuration connection is available in a file with the name of ‘settings.py’. It is exist in the root folder of the project. The following is the content of it which is referring to the database configuration connection :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'django_postgres',
        'NAME': 'sysapp',
        'PASSWORD': 'password',
    }
}

Solution

There is a solution for the above error message appear. Actually, the error message is very clear. It is about the non-existence of a database. That database name is ‘sysapp’ as it exist in the database configuration connection in the ‘settings.py’ file. The following are the step for solving the problems :

  1. First of all, just access the database just to go further for checking the existence of the database. The following is the command for logging and accessing the database. Specifically, it is the PostgreSQL database server :

    C:\>psql -Upostgres
    Password for user postgres:
    psql (14.0)
    WARNING: Console code page (437) differs from Windows code page (1252)
    8-bit characters might not work correctly. See psql reference
    page "Notes for Windows users" for details.
    Type "help" for help.
    
    postgres=# create database sysapp;
    CREATE DATABASE
    postgres=# \q
    
    C:\>
    
  2. After that, just check all the available databases by typing the following command :

    postgres=# \l
    List of databases
    Name       | Owner    | Encoding | Collate                    | Ctype                      | Access privileges
    -----------+----------+----------+----------------------------+----------------------------+-----------------------
    postgres   | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
    template0  | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres +
               |          |          |                            |                            | postgres=CTc/postgres
    template1  | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres +
               |          |          |                            |                            | postgres=CTc/postgres
    (4 rows)
    
    postgres=#
  3. As in the output of the above command execution, the database with the name ‘sysapp’ is not exist. So, create a database with the name ‘sysapp’ as follows :

    postgres=# create database sysapp;
    CREATE DATABASE
    postgres=#
    
  4. Finally, just run back the Django internal service once more. If there are no more error messages appearing to prevent it for running, the Django internal service will run and receive incoming request to access the Django web application normally.

Leave a Reply