Handle unique conflicts by updating instead of failing.
Section: Insert, Update, Delete, and Merge
Upsert with ON CONFLICT
sql
sql
INSERT INTO public.users (email)
VALUES ('a@example.com')
ON CONFLICT (email) DO UPDATE
SET created_at = now();Explanation
See summary for usage details.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Insert, Update, Delete, and Merge
Insert with RETURNING
Insert rows and immediately return generated values.
Update from another table
Update rows using another table in the same statement.