Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional insert into postgres DB from CSV

Tags:

postgresql

I want to import a CSV into a Postgres table, but I want to be able to decide on the fly whether or not a column should be a 0 or a 1 depending on existing rows in the table.

For example, I'm adding contacts to a table and want to mark them as primary if no contact exists already that is primary, otherwise add them as secondary:

Existing rows:

contact_id | branch_id | primary
-------------+-----------+--------
1          | 100       | 1
2          | 101       | 1
3          | 101       | 0

CSV data,

contact_id | branch_id
-----------+-----------
4          | 100
5          | 101
6          | 102
7          | 103

Desired result,

contact_id | branch_id | primary
-----------+-----------+--------
1          | 100       | 1
2          | 101       | 1
3          | 101       | 0
4          | 100       | 0
5          | 101       | 0
6          | 102       | 1
7          | 103       | 1

Notice contact 4 and 5 gets added as secondary as primary contacts already exist for those branches, whereas 6 and 7 get added as primary as no primary contacts exist for those branches.

Is this possible with postgres 9.2?

like image 639
Chris McKinnel Avatar asked Sep 10 '26 21:09

Chris McKinnel


1 Answers

I would implement this in two stages using a PL/pgSQL function and a temporary table. Basically:

  1. create a temporary table with the columns corresponding to the CSV file
  2. COPY FROM your CSV file into that temp table
  3. insert into the final table based on a SELECT from the temp table LEFT JOINed to existing rows in the final table, so you can ascertain whether a primary row already exists
  4. drop the temp table

(Incidentally, this means you can use a SECURITY DEFINER function, created as root, but run by a less privileged user, with the CSV filename hard-coded, rather than running the entire import as root.)

like image 159
IMSoP Avatar answered Sep 12 '26 16:09

IMSoP