How to UPSERT (MERGE, INSERT … ON DUPLICATE UPDATE) in PostgreSQL?

A very frequently asked question here is how to do an upsert, which is what MySQL calls INSERT … ON DUPLICATE UPDATE and the standard supports as part of the MERGE operation. Given that PostgreSQL doesn’t support it directly (before pg 9.5), how do you do this? Consider the following: CREATE TABLE testtable ( id … Read more