IntermediateINSERT, UPDATE, DELETE

Conditional INSERT with ON CONFLICT DO NOTHING

The query

SQL
INSERT INTO customers (email, name, city, country)
VALUES ('new@example.com', 'New Customer', 'Pune', 'India')
ON CONFLICT (email) DO NOTHING; -- skip if email exists

-- Or update specific fields:
INSERT INTO customers (email, name, city, country)
VALUES ('existing@example.com', 'Updated Name', 'Mumbai', 'India')
ON CONFLICT (email) DO UPDATE SET
  name = EXCLUDED.name,
  city = EXCLUDED.city
WHERE customers.name != EXCLUDED.name;
Tested against PostgreSQL 16

Note

DO NOTHING silently ignores conflicts. DO UPDATE (UPSERT) updates matching rows.

Tables referenced