Created
January 16, 2018 19:15
-
-
Save Sillson/64a939173e34e301ef82bd90563e7f49 to your computer and use it in GitHub Desktop.
Drop all Postgres Constraints that match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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