Irshadmi4
Irshadmi4

Reputation: 500

Delete all table without dropping database in postgres in django dbshell in one command?

I tried this ..

 select 'drop table if exists "' || tablename || '" cascade;' 
from pg_tables
 where schemaname = 'public';

but doesn't seems to work out for one command?

Upvotes: 26

Views: 71361

Answers (2)

Chocksmith
Chocksmith

Reputation: 1208

Run the following bash script:

psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop table \"' || tablename || '\" cascade;' from pg_tables where schemaname='public'" | psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db>

I copied from here: http://www.commandlinefu.com/commands/view/12989/postgresql-drop-all-tables-from-a-schema

It worked for me.

Upvotes: 11

Haji
Haji

Reputation: 2087

If all of your tables are in a single schema, this approach could work (below code assumes that the name of your schema is 'public')

drop schema public cascade;
create schema public;

Drop all tables in PostgreSQL?

see above link for more answers

Upvotes: 66

Related Questions