Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to reset Django PostgreSQL database?

I was able to reset a Django PostgreSQL database using the following steps:

  1. Delete migration files
  2. Enter psql command prompt. Connect to database. drop schema public cascade; create schema public;
  3. Step 2 unfortunately seemed to have removed my user role and rights so I went back to the psql command promt and recreated those.
  4. Step 2 also meant I needed to run the following commands in the psql command prompt: grant usage on schema public to public; grant create on schema public to public;
  5. Step 2 also deleted the standard Django users I had created so I needed to recreate these
  6. python manage.py makemigrations && python manage.py migrate

I am currently making changes to my models and testing each change. I don't have any data I need to keep. Is there an easier way than the above to reset the database when migrations donät work?

I would at least like to replace step 2 with something else so that I can skip steps 3-5.

like image 272
user1283776 Avatar asked Jan 03 '16 11:01

user1283776


People also ask

How do I clear PostgreSQL database?

The first method to remove a PostgreSQL database is to use the following SQL statement: DROP DATABASE <database name>; The command removes the directory containing the database information and the catalog entries. Only the database owner can execute the DROP DATABASE command.

How delete all data Django?

If you want to remove all the data from all your tables, you might want to try the command python manage.py flush . This will delete all of the data in your tables, but the tables themselves will still exist.

How do I reset Django migrations?

Reset the Whole Database in Django sqlite3 and then delete all the migrations folders inside all the apps. After deleting the migrations folders, we can remake the migrations and migrate them using two commands; namely, python manage.py makemigrations and python manage.py migrate .


1 Answers

Probably the easiest way to do this is to recreate the whole database. In Ubuntu, it looks like this:

sudo su postgres psql drop database your_database_name; create database your_database_name with owner user_you_use_in_django; \q exit 

That's it. You have clean database. To make it ready to go, you have to run migrations with python manage.py migrate.

If you are working on your project alone, you can delete and recreate migrations, if you want.

like image 65
Igor Pomaranskiy Avatar answered Sep 21 '22 21:09

Igor Pomaranskiy