Skip to content

Instantly share code, notes, and snippets.

@nuxero
Created February 21, 2018 17:55
Show Gist options
  • Save nuxero/89ba38aec25e073509a39e7a4f8edc80 to your computer and use it in GitHub Desktop.
Save nuxero/89ba38aec25e073509a39e7a4f8edc80 to your computer and use it in GitHub Desktop.
truncate all tables postgresql
CREATE OR REPLACE FUNCTION truncate_tables(username IN VARCHAR) RETURNS void AS $$
DECLARE
statements CURSOR FOR
SELECT tablename FROM pg_tables
WHERE tableowner = username AND schemaname = 'public';
BEGIN
FOR stmt IN statements LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
END;
$$ LANGUAGE plpgsql;
SELECT truncate_tables('MYUSER');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment