You have basically two options, depending on your data and fkey constraints.
If there are no fkeys to the table, then the best thing to do is to truncate the table before loading it. Note that truncate behaves a little odd in transactions so the best thing to do is (in a transaction block):
- Lock the table
- Truncate
- Load
This will avoid other transactions seeing an empty table.
If you have fkeys then you may want to load into a temporary table and then do an upsert. In this case you may still want to lock the table to avoid a race condition if it is possible other transactions may want to write to the table (also in a transaction block):
- Load data into a temporary table
- Lock the destination table (optional, see above)
- use a writeable cte to "upsert" in the table.
- Use a separate delete statement to delete data from the table.
Stage 3 is a little tricky. You might need to ask a separate question about it, but basically you will have two stages (and write this in consultation with the docs):
- Update existing records
- Insert non-existing records
Hope this helps.
COPYstatements. You could precede the data load withTRUNCATE <table>, or add that to the top of your dump.sql file.pg_dumpwith--cleanto "Output commands to clean (drop) database objects prior to outputting the commands for creating them."