简体   繁体   中英

Conditional insert into postgres DB from CSV

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?

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 JOIN ed 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 .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM