7

I am trying to move my django project into a production environment and in doing so I switched from using sqlite to postgres. In my development environment, whenever I made changes to my models or anything that would significantly change how the database was setup, I would literally just drag my sqlite file to the trash and just run syncdb to create a new empty one (probably bad practice). Now that I am using postgres, I am wanting to do the same thing without actually deleting the database. Basically I was wondering if there was a way to completely empty it or clear it out and then just run syncdb and start over?

I also welcome any alternative suggestions that might lead me down the right path, I'm very new to this.

1

3 Answers 3

8

You can use flush. Just run this command:

python manage.py flush
Sign up to request clarification or add additional context in comments.

5 Comments

So I could make changes to my existing models and add new ones, and then just use the flush command and syncdb?
what is "initial data"
U won't need to use syncdb while using flush. Flush clears out the data from db but not the structure
the link is broken
Yes, it should work on every table regardless of any databases.
5

First if you have initial data in your database you can use dumbpdata command:

python manage.py dumpdata > initial_data.json

For specific app run:

python manage.py dumpdata <app_name> > initial_data.json

Second run the flush command to clean your database:

python manage.py flush

Third and last, run loaddata command to load the initial data into your database and create superuser by running createsuperuser command

python manage.py loaddata initial_data.json
python manage.py createsuperuser

1 Comment

What is the 'initial data' ? I am trying to remove ALL of the data, not specific app data.
0

In case flush does not work, you can drop the whole database.

Go to windows command line. If server is 'postgres' and db name is 'mydb', run:

  1. C:\> psql -U postgres

  2. You will see a postgres-# prompt. Next is to close connections running the following:

    SELECT * FROM pg_stat_activity WHERE pg_stat_activity.datname='mydb'; SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb';

  3. Drop database once for all: DROP DATABASE mydb;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.