I'm migrating existing test environment to Amazon RDS PostgreSQL. The test framework has a feature of reloading data in certain tables to an earlier state. For this it disables foreign keys, deletes existing data, loads save state and enables foreign keys again.
Currently, the test framework disables foreign keys by disabling all triggers (of course, this requires superuser):
alter table tablename disable trigger all;
On RDS, this fails with:
ERROR: permission denied: "RI_ConstraintTrigger_a_20164" is a system trigger
How can I temporarily disable foreign keys in Amazon RDS PostgreSQL?
Note: similar question has already been asked (PostgreSQL on RDS: How to bulk import data with FK constraints?) but it was specifically about offline import and the solution is specific to offline import too.
Best Answer
session_replication_role
I found an alternative way of disabling foreign keys -- https://stackoverflow.com/a/18709987
And re-enabling them with
This works on RDS but still requires unusual privileges (i.e. not granted by default).
dropping and recreating FKs
Alternative solution is, as suggested in comments, to temporarily drop the FKs. This brings additional advantage that data is verified when FKs are re-enabled.
Dropping
Recreating