0PricingLogin
SQL Academy · Lesson

UPSERT: ON CONFLICT DO UPDATE (PostgreSQL)

Implement idempotent inserts with ON CONFLICT DO UPDATE, use EXCLUDED, and choose the right conflict target.

What Is UPSERT?

UPSERT = INSERT if not exists, UPDATE if it does. PostgreSQL spells it INSERT ... ON CONFLICT.

ON CONFLICT DO NOTHING

Insert if missing, silently skip if duplicate:

INSERT INTO users (email, full_name)
VALUES ('alice@example.com', 'Alice')
ON CONFLICT (email) DO NOTHING;

All lessons in this course

  1. INSERT with Multiple Rows
  2. UPSERT: ON CONFLICT DO UPDATE (PostgreSQL)
  3. UPDATE with FROM and JOIN-style Updates
  4. DELETE with USING and Safe Patterns
← Back to SQL Academy