Yingce
Yingce

Reputation: 275

How I do destroy all tables for schema in PostgreSQL

I have some tables in schema public's;

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

Upvotes: 0

Views: 239

Answers (2)

alibaba
alibaba

Reputation: 1903

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

DROP SCHEMA public CASCADE;

CREATE SCHEMA public;

Upvotes: 0

stas.yaranov
stas.yaranov

Reputation: 1787

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.

Upvotes: 3

Related Questions