I'm trying to put together a solution where I have a bunch of script files that contain my schema creation. Something similar to Visual Studio's SQL Project, but for Postgres.

My problem is that the files won't necessarily be read in the correct order, for instance the first file/script/table read has a dependency on the 2nd file/script/table.

I was hoping there was some way I could disable the check to see if the referred table/column does exist, creating all the tables, and then re-enabling the check.

I'm running 9.2.24

My 2nd solution would be to order the files in such a way that it does only create a table when the dependencies already exist, but my above mention method would be preferred.

  • 1
    Unrelated to the problem, but Postgres 9.2 is no longer supported you should plan an upgrade as soon as possible.
    – user1822
    Aug 14, 2019 at 15:07

1 Answer 1


Not possible. The source and target of a foreign key constraint must exist when you create the foreign key.

The usual solution to that problem is, to have all foreign keys in a separate script that is run after all tables have been created. Not together with each table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.