Bijan
Bijan

Reputation: 6772

How can I use postgres' COPY FROM correctly?

I have a csv file with headers. 4 columns.

I need the first 3 columns in table A, and the last column in table B.

Table A has an ID, which should be set by default (not from columns).

Here's what I've tried:

COPY a FROM '/home/bijan/Downloads/test_events.csv' WITH CSV HEADERS

The (first) problem I run into with this is it's not setting the default value for the ID of the table 'A', instead it thinks the first column of the CSV is the first column in the table (which should be default).

Thank you for your time. :)

Upvotes: 2

Views: 3022

Answers (1)

Magnus Hagander
Magnus Hagander

Reputation: 25078

You're probably best off loading it into a temporary table and then sending that one to the two different other tables - you cannot load into two tables in one operation.

You can, however, control which columns the data goes into (thus skipping the id column), with something like

COPY a(col2,col3,col4) FROM '/home/bijan/Downloads/test_events.csv' WITH CSV HEADERS

Upvotes: 5

Related Questions