There are two things you can do (these are complementary, not alternatives):
- Create your foreign key constraints as DEFERRABLE. Then, call “SET CONSTRAINTS DEFERRED;”, which will cause foreign key constraints not to be checked until the end of the transaction. Note that the default if you don’t specify anything is NOT DEFERRABLE (annoyingly).
- Call “ALTER TABLE mytable DISABLE TRIGGER ALL;”, which prevents any triggers executing while you load data, then “ALTER TABLE mytable ENABLE TRIGGER ALL;” when you’re done to re-enable them.