How to fix – django.db.utils.OperationalError?

These following are some possible fixes that you can try out to solve this problem.

Check Database Settings.

Open settings.py file in django project. In that we have defined Databases setting. make sure it is available and correctly defined. In below image if we used database other than sqllite3 then we must have to correctly define these settings in order to get connected to database.

Missing Migrations

If some migrations are not migrated then it may lead to this error. so for this you can remigrate the database from start.

  • Delete database file (e.g db.sqlite3).
  • Delete migrations folders present in the app folders.
  • python manage.py runserver” run this command. (this will recreate the database file).
  • Run “python manage.py makemigrations” command from the terminal.
  • Run “python manage.py migrate
  • Run “python manage.py runserver” from the terminal.

Check Permissions & Grant Access

Suppose that you are trying to perform certain action on database tables but due to permission issues it may also lead to operational error. You can try out following code to get the previlages already given to the table. if there is no read/write access given then you can grant those access.

  • Open Database query tool and connect to the database that you are using in project. Following example shows how you can check and grant all previlages to the database.

  • Open query editor and run following commands. Enter your username at the place of root and host name at the place of localhost.

  • You will this as a result when you succcessfully check and grants the previlates to the database.

Restart the Database.

you can restart the database service to create the schema and tables again. From terminal you can hit following commnds to do it.

python manage.py flush

this will delete all the data from the table. Now run migrations again to restart with a fresh schema and empty data.

python manage.py migrate


OperationalError in Django

In this article first, we explore the problem of the ‘django.db.utils.OperationalError’ error, Why this error occurs ? and its possible fixes.

Similar Reads

What is ‘django.db.utils.OperationalError’?

You may see the following error when running the django project:...

How to fix – django.db.utils.OperationalError?

These following are some possible fixes that you can try out to solve this problem....

Contact Us