Skip to content
Advertisement

INSERT ON CONFLICT DO UPDATE using pg-promise helpers for multi row insert/update

I am trying to insert multiple rows to pgsql database using pg-promise. In my case, few records, which I am trying to insert, may exist already in the table. In such case, I need to update them. By going through the pg-promise official documentaion, I found multiple rows can either be inserted or updated using helpers. Is there any way to do like below but for multiple inserts/updates?

Advertisement

Answer

Create your static variables somewhere:

In the example below we assume your data is an array of valid objects:


Extras

If you want all SQL generated in upper case rather than low case, you can set option capSQL:

And the best way to see what’s generated and executed is with the help of pg-monitor.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement