31

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.

7 Answers 7

48

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.

2
  • 1
    Thank you! But that doesn't simplify the steps in my OP much. I still need to recreate default django users, and still need to alter user roles to what I want them to be. Jan 3, 2016 at 13:05
  • 1
    If you need some data to be present after database recreation, you can use fixtures, or some automation scripts. Jan 3, 2016 at 13:28
9

I successfully "reset my django postgreSQL database" (while not losing data).

Updating my models.py and then using ./manage.py makemigrations and ./manage.py migrate wasn't possible because the table(s) I needed to rename pointed to one another circularly. Django was not unable to "auto-magically" accomplish said task.

So, I opted to backup my data. Flush. Delete the tables. Update my models.py file. Modify the (json) backup I created (using ./manage.py datadump > fileName.json) to reflect my updated column names/table names. Then, I recreated the database using ./manage.py migrate and ./manage.py makemigrations. Then I loaded my backup back into the database's newly created tables using ./manage.py loaddata fileName.json.

The exact steps I took:

  1. First I took a backup of my server/VM.
  2. ./manage.py dumpdata > db.json
  3. ./manage.py flush
  4. Choose "Yes" [NOTE THIS DELETES ALL YOUR DATA!!!]
  5. Delete all tables from --myDataBaseName--
  6. Delete your migrations folder
  7. Make your changes (if you have any) to your model.py file
  8. ./manage.py migrate
  9. ./manage.py makemigrations --myAppName--
  10. ./manage.py migrate --myAppName--
  11. go into db.json and remove contenttype related stuffz. You may also need to remove (and seed manually after step 12 in some cases) auth_permissions related entries in your db.json. Save modified db.json as db_new.json so that you don't lose your original backup in case things get fubar'd and you need to put everything back to how it was!
  12. ./manage.py loaddata db_new.json
9

With Python

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute("DROP SCHEMA public CASCADE;")
    cursor.execute("CREATE SCHEMA public;")

then,

python manage.py migrate
8

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

2
  • 8
    This was the right starting place for me! I needed to adapt it a little bit: docker-compose down -v, where the -v means to also delete volumes; docker-compose up --detach db, which is my command to bring the (now empty) database back up; and then the same python manage.py migrate.
    – Ezra
    Aug 14, 2020 at 22:00
  • -v was the answer to my problem .Thanks!
    – Aaron
    Dec 2, 2020 at 18:39
2

If you want a completely clean start, you're gonna wanna stick to dropping the DB. Which means the steps you mention - recreate it, add privileges, re-generate all the migrations and re-run them.

Good news is that you can easily make all this into a single/few line commands.

Fresh migration files

If you delete the whole folders, you're gonna have to run the makemigrations command mentioning all the app names. That's a hassle if you do this often. To have Django see the apps that need migrations, you'll wanna keep the migrations folder and the __init__.py inside them.

Here's a bash command for that:

find . -path "*migrations*" -not -regex ".*__init__.py" -a -not -regex ".*migrations" | xargs rm -rf

Then the usual (this should create migrations for all the apps that had migrations before):

python manage.py makemigrations

Resetting the PostgreSQL DB

Run this in the console:

psql -c "drop database <db_name>;"
psql -c "create database <db_name>;"
psql -c "grant all on database <db_name> to <db_user>;"

And then finally re-run migrations with

python manage.py migrate

Superuser

You're gonna obviously be missing a superuser, so you might wanna also do:

python manage.py createsuperuser

No-input way of doing that is piping python code into the shell:

echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'badmin@myproject.com', 'pa$$w0rd')" | python manage.py shell

Generally speaking about these very common actions - Do yourself a favour and write a bit of bash. It has saved me many, many accumulated hours over the years of working with not only Django. Because even better than a oneline command is having a whole utility file to store more of these handy functions. Then you can just run something like:

django --reset_migrations
db --reset <my_db>
django --migrate

Or even aggregate that into a single line if you find yourself repeating the same few actions.

reset_django() {
    find . -path "*migrations*" -not -regex ".*__init__.py" -a -not -regex ".*migrations" | xargs rm -rf
    python manage.py makemigrations
    psql -c "drop database <db_name>;"
    psql -c "create database <db_name>;"
    psql -c "grant all on database <db_name> to <db_user>;"
    python manage.py migrate
    echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'badmin@myproject.com', 'pa$$w0rd')" | python manage.py shell
}

My utilities for inspiration

#!/bin/bash


django() {

    project_name=$(basename $PWD)
    project_path="$PWD"
    manage_path="${project_path}/${project_name}/manage.py"

    if [ ! -f $manage_path ] ; then  # No project/manage.py
        echo "Error: Could not locate Django manage.py file."
        return -1
    fi

    if [ $# -eq 0 ] ; then
        echo "Django project detected."
    fi

    while [ ! $# -eq 0 ]
        do
            case "$1" in

                --help | -h)
                        echo "Django shortcut, unknown commands are forwarded to manage.py"
                        echo "  -c, --check         Run Django manage.py check."
                        echo "  --req           Install requirements."
                        echo "  -r, --run           Run server."
                        echo "  -s, --shell         Run Django shell plus."
                        echo "  -sd, --shell            Run Django shell plus. Debug DB (print sql)"
                        echo ""
                    ;;

                --check | -c)
                        python $manage_path check
                    ;;

                --shell | -s)
                        python $manage_path shell_plus --bpython
                    ;;

                --shell | -sd)
                        python $manage_path shell_plus --bpython --print-sql
                    ;;

                --run | -r)
                        python $manage_path runserver
                    ;;

                --req)
                        pip install -r $project_path/requirements.txt
                    ;;

                --mig | -m)
                        python $manage_path makemigrations
                        python $manage_path migrate
                    ;;

                --reset_migrations)
                        find . -path "*migrations*" -not -regex ".*__init__.py" -a -not -regex ".*migrations" | xargs rm -rf
                        python $manage_path makemigrations
                        ;;

                *)
                    python $manage_path "$@"
                    ;;

            esac
            shift
        done

}
1

I don't understand why you think you need to delete either migrations or the database. The whole point of migrations is that they build on what has gone before; you should never need to delete and start again. Just do makemigrations every time you make a change, and the series of migrations should always work.

Later, you may want to squash a set of migrations into one, for the sake of speed, which is again supported natively. But never delete the migrations you have already run.

7
  • 3
    I wanted to delete migrations because I got an error when running manage.py migrate. I was not able to figure out how to fix it. Jan 3, 2016 at 12:59
  • 8
    If you don't have a database on production yet, removing the migrations and creating them again does not matter much.
    – cdvv7788
    Jan 3, 2016 at 16:34
  • 11
    I think this is the wrong attitude. Developers should not be fearful of ANY code. They need to have full control over the process especially opinionated and at times unwieldy frameworks. To solve a similar issue over converting types which is a purely internal issue to Django, I went back and rewrote (migration) history. Governments do it all the time, why shouldn't developers?
    – codervince
    Mar 30, 2016 at 0:49
  • 1
    Bottom line Daniel, sometimes, ./manage.py makemigrations xyzAppName doesn't properly work. Or, the migration file generated by it doesn't end up properly running when passed to `./manage.py migrate xyzAppName'. For example, circularly dependent tables (were someone to want them for some reason) aren't easily modify-able.
    – Xandrix
    Jan 16, 2020 at 3:38
  • I'm on Daniel's side here. Deleting migrations can mess up things badly when you have a production db... been there... They are called migrations because "the whole point of migrations is that they build on what has gone before". I totally agree with that..
    – Micromegas
    May 22, 2020 at 20:41
-2

first you need to remove the migration file from your project. And then run the psql database using terminal window(make sure anything related to the database and projects should be closed so, that no instance of the db should be running). Then follow the following link to remove the db from the terminal window https://www.postgresqltutorial.com/postgresql-drop-database/

hope this answers the problem. I myself ran into the same problem and this steps fixed it. Happy developing!

5
  • This is just a different, less accurate description of what's in the question. Again, with this unspecified need that removing migration files has anything to do with dropping or recreating a database.
    – user1600649
    Jul 1, 2020 at 13:25
  • since I am very new into computer science this method seems to be more apt for me and after deleting the db we can recreate it from scratch. I will be glad if you please share the appropriate answer to the question. Jul 1, 2020 at 13:41
  • Your answer uses different words, but is otherwise the same as this answer.
    – user1600649
    Jul 1, 2020 at 13:47
  • 1
    also try not to use links like that since they can disappear and then your answer would no help anyone. A good way to handle this is to quote the part of the article that helps and then leave the link here anyway Jul 1, 2020 at 14:02
  • @SamApostel sure, this insight was much needed Jul 1, 2020 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.