PostgreSQL – How to use “upsert”?

If data exists, do nothing (don’t INSERT)

INSERT INTO customers ("name", "email")
      VALUES('Microsoft','hotline@microsoft.com') 
      ON CONFLICT ("name") DO NOTHING;

If name column has same data, do nothing

If data exists, UPDATE row

INSERT INTO customers ("name", "email")
    VALUES('Microsoft','hotline@microsoft.com') 
    ON CONFLICT ("name") 
        DO 
        UPDATE SET "email" = EXCLUDED.email || ';' || customers.email;

Before

After

Be the first to comment

Leave a Reply

Your email address will not be published.


*