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.

david
  • 6,303
  • 16
  • 54
  • 91

3 Answers3

8

You can use flush. Just run this command:

python manage.py flush
ruddra
  • 50,746
  • 7
  • 78
  • 101
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
Emad Mokhtar
  • 3,237
  • 5
  • 31
  • 49
  • 1
    What is the 'initial data' ? I am trying to remove ALL of the data, not specific app data. – david Feb 11 '15 at 17:02
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;

user3507584
  • 3,246
  • 5
  • 42
  • 66