Vlad Ankudinov
Vlad Ankudinov

Reputation: 2046

PostgreSQL v9.5+ UPSERT

It looks like PostgreSQL v9.5 got support for INSERT ... ON CONFLICT DO NOTHING/UPDATE http://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=168d5805e4c08bed7b95d351bf097cff7c07dd65

I want to do something like this

UPSERT INTO "myScheme"."myTable" (id, a, b, c) VALUES ($1, $2, $3, $5) WHERE id = $4'

How to achive this result taking into considiration that I am using PostgreSQL v9.5+

Upvotes: 2

Views: 147

Answers (1)

Ildar Musin
Ildar Musin

Reputation: 1468

It could be like this:

INSERT INTO myScheme.myTable(id, a, b, c) VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO
UPDATE SET a=$2, b=$3, c=$4;

If there is no record with id=$1 then it will do insert. Otherwise it will update existing record.

Upvotes: 3

Related Questions