Skip to content

Instantly share code, notes, and snippets.

@daniel-illi
Last active November 2, 2016 09:30
Show Gist options
  • Select an option

  • Save daniel-illi/8584ad20282abb62e123da643a82157c to your computer and use it in GitHub Desktop.

Select an option

Save daniel-illi/8584ad20282abb62e123da643a82157c to your computer and use it in GitHub Desktop.
Postgres truncate_tables
CREATE OR REPLACE FUNCTION truncate_tables(username IN VARCHAR) RETURNS void AS $$
DECLARE
statements CURSOR FOR
SELECT schemaname, tablename FROM pg_tables
WHERE tableowner = username AND schemaname = 'public';
BEGIN
FOR stmt IN statements LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.schemaname) || '.' || quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment