数据库

删除PostgreSQL模式中的所有表


执行以下查询可以删除指定模式中的所有表。 将 my-schema-name 替换为您的模式名称。在 Supabase 中,默认模式是 public

1
2
3
4
5
6
7
do $$ declare r record;begin for r in (select tablename from pg_tables where schemaname = 'my-schema-name') loop execute 'drop table if exists ' || quote_ident(r.tablename) || ' cascade'; end loop;end $$;

该查询的工作原理是列出给定模式中的所有表,然后为每个表执行 drop table 操作(因此使用了 for... loop)。

您可以通过 Supabase 仪表板中的 SQL 编辑器运行此查询,或者如果您直接连接到数据库,也可以通过 psql 运行。