Created
January 4, 2025 12:49
-
-
Save gentildpinto/77c6cb552f51d6cf59cf36b9908f5c63 to your computer and use it in GitHub Desktop.
Truncate PostgreSQL Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO $$ | |
DECLARE | |
r RECORD; | |
BEGIN | |
-- Disable all triggers temporarily | |
SET session_replication_role = 'replica'; | |
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') | |
LOOP | |
EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE'; | |
END LOOP; | |
-- Re-enable triggers | |
SET session_replication_role = 'origin'; | |
END $$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment