Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a CSV to postgresql that already has ID's assigned?

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?

like image 274
yburyug Avatar asked Dec 14 '25 12:12

yburyug


1 Answers

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_called field to true, meaning that the next nextval will advance the sequence before returning a value.

Bold emphasis mine.

like image 121
Erwin Brandstetter Avatar answered Dec 16 '25 06:12

Erwin Brandstetter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!