Skip to content
masterclass Buddy

PostgreSQL · Command

ON CONFLICT

ON CONFLICT handles a unique or exclusion conflict during INSERT. It can skip the row or update the existing row, making an atomic insert-or-update operation.

Last updated

Syntax

[ WITH [ RECURSIVE ] with_query [, ...] ] INSERT INTO table_name [ AS alias ] [ ( column_name [, ...] ) ] [ OVERRIDING { SYSTEM | USER } VALUE ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query } [ ON CONFLICT [ conflict_target ] conflict_action ] [ RETURNING [ WITH ( { OLD | NEW } AS output_alias [, ...] ) ] { * | output_expression [ [ AS ] output_name ] } [, ...] ]

In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.

Subscribe
  • INSERT
  • ON CONFLICT DO NOTHING
  • ON CONFLICT DO UPDATE
  • RETURNING
  • MERGE
  • unique index inference
  • EXCLUDED
  • Read Committed Isolation Level

Source: PostgreSQL: Documentation: 18: INSERT. Checked 2026-11-03.