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
- INSERT with Multiple Rows
- UPSERT: ON CONFLICT DO UPDATE (PostgreSQL)
- UPDATE with FROM and JOIN-style Updates
- DELETE with USING and Safe Patterns