如何删除postgresql数据库中的所有表

如何使用脚本删除postgresql数据库中的所有表

回复

共1条回复 我来回复
  • xiaozhch5
    xiaozhch5
    这个人很懒,什么都没有留下~
    评论

    将下述脚本保存为droptable.sql


    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 $$;

    然后进入数据库执行上述脚本:

    \i droptable.sql

    即可删除数据库中所有的表

    1年前 0条评论