On this page
  1. ON CONFLICT
  2. Syntax & Overview
  3. Notes & Best Practices
  4. Example

ON CONFLICT

Prev: `RETURNING` | Up: clauses | Next: `CREATE TABLE`
Rel: clauses, SQL Query Execution Lifecycle
Ref: PostgreSQL — SQL Commands
Tags: #public #databases #sql #clauses #tier/standard


Syntax & Overview

INSERT INTO kv (k, v) VALUES ('a', '1') ON CONFLICT (k) DO UPDATE SET v = excluded.v;

Atomically resolves unique/primary key conflicts without check-then-insert races.

Notes & Best Practices

Example

INSERT INTO settings (key, value)
VALUES ('theme', 'dark')
ON CONFLICT (key) DO UPDATE
SET value = EXCLUDED.value;

Linked from

Related notes