How to Upsert in PostgreSQL (INSERT ... ON CONFLICT)

Last updated July 24, 2026 · By the SaturnSQL team

INSERT ... ON CONFLICT (key) DO UPDATE inserts a row or updates the existing one when a unique constraint matches. EXCLUDED refers to the row you tried to insert.

INSERT INTO customers (id, email, updated_at)
VALUES (101, '[email protected]', now())
ON CONFLICT (id) DO UPDATE SET
  email = EXCLUDED.email,
  updated_at = now();

Insert-if-missing only

INSERT INTO customers (id, email)
VALUES (101, '[email protected]')
ON CONFLICT (id) DO NOTHING;

The conflict target must match a unique index or constraint. Postgres 15+ also offers MERGE for more complex conditional logic.

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Related PostgreSQL guides

© 2026 Panda Capital Oy Ab. All rights reserved.