9

Does the PostgreSQL COPY command have the option of choosing which fields to map the CSV columns to?

The PostgreSQL COPY command appears to expect that the target table matches its columns exactly. Am I missing something or is that how it actually works?

Is there some alternative command that enables that?

JustBeingHelpful
2,11618 gold badges45 silver badges61 bronze badges
asked Mar 18, 2016 at 8:59

2 Answers 2

18

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.

answered Mar 18, 2016 at 9:28
1
  • 7
    Little hint for the "intermediate table" you can use an UNLOGGED Table, to reduce WAL's and import time. Commented Mar 18, 2016 at 15:58
4

Grab yourself a copy of CSVKit

in2csv foo.csv 
 | csvcut -C "columnIDontWant,AnotherIDontWant" 
 | csvsql --db postgresql://localhost/dbname --insert --tables Foo --no-create

Or csvquote

csvquote | cut -d, -f... | csvquote - u | psql ...
answered Mar 18, 2016 at 18:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.