Skip to content

Instantly share code, notes, and snippets.

@jziggas
Created October 3, 2023 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jziggas/fbd2a7cd01e1a00c4b3435440f9e3f35 to your computer and use it in GitHub Desktop.
Save jziggas/fbd2a7cd01e1a00c4b3435440f9e3f35 to your computer and use it in GitHub Desktop.
Drop all policies on a given table in PostgreSQL
CREATE OR REPLACE FUNCTION drop_all_policies_on_table(
target_schema text,
target_table_name text
) RETURNS void LANGUAGE plpgsql AS $$
DECLARE
policy_name text;
sql_text text;
BEGIN
FOR policy_name IN (
SELECT policyname
FROM pg_policies
WHERE schemaname = target_schema AND tablename = target_table_name
)
LOOP
sql_text := format('DROP POLICY "%s" on %I.%I', policy_name, target_schema, target_table_name);
RAISE NOTICE '%', sql_text;
EXECUTE sql_text;
END LOOP;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment