← All notes

PostgreSQL upsert: ON CONFLICT and MERGE

Short answer

INSERT ... ON CONFLICT can skip or update when a unique key already exists. Newer PostgreSQL also has MERGE for conditional insert/update/delete in one statement. [S1][S2]

Sketch

INSERT INTO items (id, name) VALUES (1, 'a')
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;

Points

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).

Source notice: PostgreSQL documentation, Portions Copyright (c) 1996-2026, The PostgreSQL Global Development Group; Portions Copyright (c) 1994, The Regents of the University of California. Used under the PostgreSQL License (https://www.postgresql.org/about/licence/).

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL docs: INSERT, PostgreSQL License; PostgreSQL Global Development Group, MERGE, PostgreSQL License.

Cite this

Permanent ID: iil:note:y45bpt4m

iinventedlight. 2026. "PostgreSQL upsert: INSERT ... ON CONFLICT." iinventedlight Library, iil:note:y45bpt4m, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-upsert-on-conflict/. License: CC-BY-4.0.

@misc{iil_note_y45bpt4m,
  author = {{iinventedlight}},
  title = {{PostgreSQL upsert: INSERT ... ON CONFLICT}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/postgres-upsert-on-conflict/},
  note = {Permanent ID iil:note:y45bpt4m. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-upsert-on-conflict.md.