Skip to content

Instantly share code, notes, and snippets.

@PickledDragon
Last active March 13, 2019 07:58
Show Gist options
  • Save PickledDragon/3ddac0810309e9ffa3e2 to your computer and use it in GitHub Desktop.
Save PickledDragon/3ddac0810309e9ffa3e2 to your computer and use it in GitHub Desktop.
SELECT 'ALTER TABLE '||nspname||'.'||relname||' ADD CONSTRAINT '||conname||' '|| pg_get_constraintdef(pg_constraint.oid)||';'
FROM pg_constraint
INNER JOIN pg_class ON conrelid=pg_class.oid
INNER JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace
ORDER BY CASE WHEN contype='f' THEN 0 ELSE 1 END DESC,contype DESC,nspname DESC,relname DESC,conname DESC;
SELECT 'ALTER TABLE '||nspname||'.'||relname||' DROP CONSTRAINT '||conname||';'
FROM pg_constraint
INNER JOIN pg_class ON conrelid=pg_class.oid
INNER JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace
ORDER BY CASE WHEN contype='f' THEN 0 ELSE 1 END,contype,nspname,relname,conname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment