Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drop all Postgres Constraints that match
BEGIN transaction;
DO $$DECLARE r record;
BEGIN
FOR r IN SELECT table_name,constraint_name
FROM information_schema.constraint_table_usage
WHERE table_name IN ('table_1', 'table_2', 'table_3') /* here be the tables to select. refactor if you want to select all */
AND constraint_name ~'\d$' /* pattern matcher */
LOOP
EXECUTE 'ALTER TABLE ' || quote_ident(r.table_name)|| ' DROP CONSTRAINT '|| quote_ident(r.constraint_name) || ';';
END LOOP;
END$$;
ROLLBACK; /* or commit */
/* ALTERNATIVELY */
select indexname from pg_indexes WHERE indexname ~ '\d$'; /* find indexes/constraints matching a pattern */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.