Skip to content
Advertisement

Upsert query is not updating the the records in postgres

I am trying to Upsert into the target table from temp table but My upsert query is not updating the records in postgresql. Although it is inserting but not updating. Please find below query:

Insert into store 
Select t.* from store_temp 
where t.id not in (select a. Id 
from store a) on conflict(id) 
DO
Update
Set source = EXCLUDED.Source

Any helping hand would be really appreciated.

Advertisement

Answer

Your syntax looks correct, but I don’t think you want the where clause. Instead:

Insert into store ( . . . )
    select . . .
    from store_temp t
    on conflict (id) do update
        set source = EXCLUDED.Source;

The . . . are for the column list. I recommend being explicit in inserts.

Then you need to be sure that id is declared as the primary key or at least has a unique constraint or index.

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