salex89
9/7/2016 - 1:58 PM

Stored procedure, drop all entries in all tables in Postgres without dropping or truncating, with optional ignoring.

Stored procedure, drop all entries in all tables in Postgres without dropping or truncating, with optional ignoring.

DECLARE
    statements CURSOR FOR
        SELECT tablename FROM pg_tables
        WHERE tableowner = username AND schemaname = 'public';
BEGIN
    FOR stmt IN statements LOOP
	IF stmt.tablename != 'algorithm' THEN
		EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
	END IF;
    END LOOP;
END;
$$ LANGUAGE plpgsql;