Thursday, 26 May 2011

Drop all tables from a PostGreSQL DB

Replace dbname with the actual database name:
psql -U dbname -W -t -d dbname -c "SELECT 'DROP TABLE ' || n.nspname || '.' || c.relname || ' CASCADE;' FROM pg_catalog.pg_class AS c LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE relkind = 'r' AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c.oid)" > /tmp/droptablespsql -U dbname -W -d dbname -f /tmp/droptables

Possibly Related Posts

No comments:

Post a Comment