It is absolutely possible - the ever helpful documentation comes to the rescue, again:
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | PROGRAM 'command' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
Which means you can do something like this:
COPY my_table (mt_id, mt_name, mt_created_by, ...)
FROM 'filename' [...]
What you cannot do is to refer columns of the CSV file. To overcome this, one can create an intermediate table with the matching number and type of columns, do the COPY
into it, then do an INSERT ... SELECT ...
to the final destination. Based on an important remark from Patrick7, the intermediate table can be defined as UNLOGGED
, saving a lot of WAL overhead when the table is big.