Easy way to reset a Django PostgreSQL database?

I was able to reset create a Django PostgreSQL database by following these steps:

  • Delete migration files
  • Enter the psql command prompt. Connection to the database. common cascade of quotation patterns; Create a public schema
  • Step 2, unfortunately, removed my role and user rights, so I went back to the psql promt command and recreated them.
  • Step 2 also meant that I needed to run the following commands on the psql command line: to give the public public use of the public schema; Provide a public publication scheme;
  • Step 2 also removed the standard Django users that I created, so I needed to recreate these
  • python manage.py makemigrations && & & python manage.py migrate

I am currently making changes to my models and testing each change. I do not have any data that I need to save. Is there an easier way than the above to reset the database when the transfer works?

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

+16
django postgresql
source share
3 answers

Probably the easiest way to do this is to recreate the entire database. On 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 

What is it. You have a clean database. In order for it to be ready for work, you need to perform the migration using python manage.py migrate .

If you only work on your project, you can delete and recreate the migration if you want.

+23
source share

If you are using docker / composer, type docker-compose down and then migrate .

0
source share

I don’t understand why you think you need to delete the migration or database. The whole point of migration is that they are based on what happened before; you no longer need to delete and start over. Just do the prototyping every time you make changes, and a series of migrations should always work.

Later you can program the set of migrations in one, for the sake of speed, which is again supported initially. But never delete the migration that you have already completed.

-2
source share

All Articles