PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values postgresql postgresql

PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values


Postgres hasn't implemented an equivalent to INSERT OR REPLACE. From the ON CONFLICT docs (emphasis mine):

It can be either DO NOTHING, or a DO UPDATE clause specifying the exact details of the UPDATE action to be performed in case of a conflict.

Though it doesn't give you shorthand for replacement, ON CONFLICT DO UPDATE applies more generally, since it lets you set new values based on preexisting data. For example:

INSERT INTO users (id, level)VALUES (1, 0)ON CONFLICT (id) DO UPDATESET level = users.level + 1;