0

I have some tables in schema public's;

I want destroy all tables at schema. Like DROP TABLE public.*

JAL
  • 41,701
  • 23
  • 172
  • 300
Yingce
  • 255
  • 2
  • 9

2 Answers2

3

I usually do it like this (I've commented execution of the query, it will only be printed):

do $$
declare
    rec record;
    query text;
begin
    for rec in select * from pg_tables where schemaname = 'public'
    loop
        query = format('drop table %s.%s', rec.schemaname, rec.tablename);
        raise notice '%', query;
        --execute query;
    end loop;
end
$$ language plpgsql;

I use cool DO thing when I just want to execute some code but don't want to create a stored procedure.

stas.yaranov
  • 1,797
  • 10
  • 17
0

If all of the tables are in the same schema (public):

DROP SCHEMA public CASCADE;

CREATE SCHEMA public;

alibaba
  • 1,623
  • 2
  • 12
  • 13