I am reading through a large list of data and adding it to a PostgreSQL database. The problem is, sometimes there are duplicate values in the data I'm reading in, but sometimes they fill in data that was missing previously. To remedy this, I have added the following to my script, but it is very ugly:
INSERT INTO tab(id,col1,col2,col3,...) VALUES (i,v1,v2,v3,...)
ON CONFLICT (id)
DO UPDATE
SET
(col1,col2,col3, ...)=(
COALESCE(tab.col1, EXCLUDED.col1),
COALESCE(tab.col2, EXCLUDED.col2),
COALESCE(tab.col3, EXCLUDED.col3),
...
);
I'm hoping there's a more elegant solution than manually writing out every column in the table. I have a few more tables I need to write these for so I'd prefer if there was a more generic way to do this.
EDIT: I'm pretty new to this, and reading the docs, this may be a dumb way to do this in the first place. Please let me know if I should even be using the INSERT
command for this, it looks like maybe just UPDATE
or some form of JOIN
could accomplish the same thing?
Postgres version:
psql (PostgreSQL) 12.14 (Ubuntu 12.14-0ubuntu0.20.04.1)