I have 3 CSV files that are about 150k rows each. They already have been given ID's in the CSV and the assosciations are held within them already. Is there a simple way to skip the auto-assignment of the id value and instead use what is already in the CSV?
A serial column only draws the next number from a sequence by default. If you write a value to it, the default will not kick in. You can just COPY to the table (see @Saravanan' answer) and then update the sequence accordingly. One way to do this:
SELECT setval('tbl_tbl_id_seq', max(tbl_id)) FROM tbl;
tbl_id being the serial column of table tbl, drawing from the sequence tbl_tbl_id_seq (default name).
Best in a single transaction in case of concurrent load.
Note, there is no off-by-1 error here. Per documentation:
The two-parameter form sets the sequence's last_value field to the specified value and sets its
is_calledfield to true, meaning that the nextnextvalwill advance the sequence before returning a value.
Bold emphasis mine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With