It inserts a row, but if it violates a unique constraint/index, it updates the existing row instead. It’s a safe way to “insert or update” in one statement.
INSERT INTO users(email, name)
VALUES ('[email protected]', 'Ada')
ON CONFLICT (email)
DO UPDATE SET name = EXCLUDED.name;Expanding on the short answer — what usually matters in practice:
Here’s an additional example (building on the short answer):
INSERT INTO users(email, name)
VALUES ('[email protected]', 'Ada')
ON CONFLICT (email)
DO UPDATE SET name = EXCLUDED.name;