PostgreSQL 删除数据库下的所有表

PostgreSQL About 629 words

方法一(推荐)

生成删除所有表的语句

SELECT 'DROP TABLE IF EXISTS ' || table_name || ' CASCADE;' 
FROM pg_tables WHERE table_schema = 'public';

输出的语句拷贝再执行即可

postgres=# SELECT 'DROP TABLE IF EXISTS ' || tablename || ' CASCADE;' FROM pg_tables WHERE schemaname = 'public';
                            ?column?                             
-----------------------------------------------------------------
 DROP TABLE IF EXISTS flyway_schema_history CASCADE;
 DROP TABLE IF EXISTS example CASCADE;
(2 rows)

方法二

直接删除对应的schema

DROP SCHEMA public CASCADE;
CREATE SCHEMA public;

PostgreSQL 9.3或更高版本,可能还需要恢复权限。

GRANT ALL ON SCHEMA public TO postgres;
Views: 46 · Posted: 2025-01-22

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh