INSERT ... ON CONFLICT

INSERT ... ON CONFLICT inserts a row or, if it violates a uniqueness constraint, does nothing (DO NOTHING) or updates the existing row (DO UPDATE). It is an atomic "upsert", safe under concurrent access. EXCLUDED refers to the values you were trying to insert.

Example

INSERT INTO stock (sku, qte) VALUES ('A1', 5)
ON CONFLICT (sku) DO UPDATE SET qte = stock.qte + EXCLUDED.qte;

Related concepts

The whole glossary