Skip to content

Instantly share code, notes, and snippets.

@Sillson
Created January 16, 2018 19:15
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 Sillson/64a939173e34e301ef82bd90563e7f49 to your computer and use it in GitHub Desktop.
Save Sillson/64a939173e34e301ef82bd90563e7f49 to your computer and use it in GitHub Desktop.
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