Remove every user-defined table from a PostgreSQL database in one operation.
Run DROP SCHEMA public CASCADE;
followed by CREATE SCHEMA public;
. This wipes every table, view, and sequence in a single transaction, then recreates the default schema.
Use pg_dump -Fc yourdb > yourdb.before_drop.dump
. The custom format keeps a compressed, restorable snapshot you can reload with pg_restore
if anything goes wrong.
Choose per-table drops when you need to keep extensions, custom types, or data in other schemas. Dynamic SQL or psql meta-commands can generate the list safely.
Yes. In psql run \gexec
to execute query output as commands:
SELECT 'DROP TABLE IF EXISTS "' || tablename || '" CASCADE;'
FROM pg_tables
WHERE schemaname = 'public';
\gexecThis drops only tables, leaving views intact.
Run ALTER SEQUENCE
for each sequence or allow pg_dump --serializable-deferrable
to rebuild them when you reload data.
You need ownership of the schema or superuser rights. Without them, DROP SCHEMA
or DROP TABLE
will fail with an “insufficient privilege” error.
\dt
or SELECT
from pg_tables
Yes. DROP SCHEMA public CASCADE;
removes every object in the public schema at once.
No. Extensions live in pg_catalog
or their own schemas and remain untouched unless explicitly dropped.
Restore the pre-drop backup: createdb newdb
then pg_restore -d newdb yourdb.before_drop.dump
.