PostgreSQL copy command generate primary key id

The COPY command should do that all by itself if your table uses a serial column for the id:

If there are any columns in the table that are not in the column list, COPY FROM will insert the default values for those columns.

So you should be able to say:

copy table_name(city, zipcode) from ...

and the id will be generated as usual. If you don’t have a serial column for id (or a manually attached sequence), then you could hook up a sequence by hand, do your COPY, and then detach the sequence.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)