Database/PostgreSQL

Postgresql 데이터베이스 안의 모든 테이블 삭제

Say simple 2024. 3. 13. 19:32
728x90
반응형

해당 쿼리는 매우 위험하므로 주의해서 사용해야 한다. curruent_schemas() 함수로 가져온 모든 테이블 명을 프로시저에서 반복문을 이용해 drop table if exists 쿼리로 삭제한다.

DO $$ DECLARE
    r RECORD;
BEGIN
    -- if the schema you operate on is not "current", you will want to
    -- replace current_schema() in query with 'schematodeletetablesfrom'
    -- *and* update the generate 'DROP...' accordingly.
    FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
        EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
    END LOOP;
END $$;
728x90
반응형