UPSERT (PostgreSQL)
UPSERT (PostgreSQL) is a SQL statement in the DML Statements category. PostgreSQL upsert using ON CONFLICT. The excluded keyword refers to the values that would have been inserted. The syntax is INSERT INTO table (columns) VALUES (values) ON CONFLICT (column) DO UPDATE SET col = excluded.col;. It returns rows affected. A typical example: INSERT INTO users (id, email, name) VALUES (1, 'new@email.com', 'Alice') ON CONFLICT (id) DO UPDATE SET email = excluded.email, name = excluded.name; -- Insert or update: excluded references proposed new values A close relative is INSERT INTO, which inserts new rows into a table. A close relative is UPDATE, which modifies existing rows in a table. Always use WHERE to avoid updating all rows. A close relative is DELETE, which removes rows from a table. Always use WHERE to avoid deleting all rows. More about this category: Data Manipulation Language — INSERT, UPDATE, DELETE, REPLACE, MERGE.