david
david

Reputation: 6805

How to "reset" postgresql database?

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.

Upvotes: 7

Views: 12790

Answers (3)

User981636
User981636

Reputation: 3629

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;

Upvotes: 0

ruddra
ruddra

Reputation: 52028

You can use flush. Just run this command:

python manage.py flush

Upvotes: 8

Emad Mokhtar
Emad Mokhtar

Reputation: 3297

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

Upvotes: 5

Related Questions