This article is half-done without your Comment! *** Please share your thoughts via Comment ***
How to drop all tables in PostgreSQL?
Recently, One of the SQL Server users asked me this question for PostgreSQL.
Because in the SQL Server we can do this kind of bulk DDL operations using sp_MSforeachtable.
SQL Server: Use sp_MSforeachtable to apply a T-SQL command to every table of current Database
In the PostgreSQL, no such functionality is available. But I am sharing two options for dropping all tables of schema or database of PostgreSQL.
If you want to drop all tables of a schema, simply drop the schema.
1 |
DROP SCHEMA schema_name CASCADE; |
Use pg_tables and prepare DROP TABLE script:
In the below script, you can modify the table/schema filter. Once you execute the below script, you can get a result of generated DROP SCRIPT which you can execute for dropping tables.
1 2 3 |
select 'drop table if exists "' || tablename || '" cascade;' from pg_tables where schemaname = 'public'; |
Leave a Reply