Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return primary keys generated from a COPY FROM statement in postgreSQL?

I'm uploading thousands of rows into my postgreSQL database and to do so I'm using the COPY FROM statement to save processing time.

I didn't found any information about that on StackExchange so I'm asking now: is it possible to retrieve the primary keys (or other column value) generated from the COPY statement like we could do with the RETURNING col_name statement of an INSERT statement ?

like image 776
kaycee Avatar asked Jun 08 '16 13:06

kaycee


1 Answers

It's not possible with COPY.

Only an INSERT with RETURNING id can give you id-s.

The fastest next to COPY approach would be a multi-row insert:

INSERT INTO table(col1, col2) VALUES (val1, val2), (val3, val4),... RETURNING id
like image 132
vitaly-t Avatar answered Sep 28 '22 07:09

vitaly-t