Skip to content
Database

删除 Postgres 模式中的所有表

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

🌐 Execute the following query to drop all tables in a given schema. Replace my-schema-name with the name of your schema. In Supabase, the default schema is public.

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

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

🌐 This query works by listing out all the tables in the given schema and then executing a drop table for each (hence the for... loop).

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

🌐 You can run this query using the SQL Editor in the Supabase Dashboard, or via psql if you're connecting directly to the database.