Skip to content

Instantly share code, notes, and snippets.

@gentildpinto
Created January 4, 2025 12:49
Show Gist options
  • Save gentildpinto/77c6cb552f51d6cf59cf36b9908f5c63 to your computer and use it in GitHub Desktop.
Save gentildpinto/77c6cb552f51d6cf59cf36b9908f5c63 to your computer and use it in GitHub Desktop.
Truncate PostgreSQL Database
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