title: PostgreSQL Upsert with a WHERE clause
body: <p>I am trying to migrate an Oracle merge query to PostgreSql. As described in <a href="http://www.postgresqltutorial.com/postgresql-upsert/" rel="nofollow noreferrer">this</a> article, Postgres UPSERT syntax supports a "where clause" to identify conditions of conflict.</p>
  
  <p>Unfortunately, that webpage does not provide an example with the "where clause". I tried searching for it elsewhere but could not find it. Hence this question.</p>
  
  <p>Following the same example in the above given webpage, here is an example setup:</p>
  
  <pre><code>CREATE TABLE customers (
      customer_id serial PRIMARY KEY,
      name VARCHAR UNIQUE,
      email VARCHAR NOT NULL,
      active bool NOT NULL DEFAULT TRUE
  );
  
  INSERT INTO customers (NAME, email) VALUES
   ('IBM', 'contact@ibm.com'),
   ('Microsoft', 'contact@microsoft.com'),
   ('Intel','contact@intel.com');
  
  
  SELECT * FROM customers;
   customer_id |   name    |         email         | active
  -------------+-----------+-----------------------+--------
             1 | IBM       | contact@ibm.com       | t
             2 | Microsoft | contact@microsoft.com | t
             3 | Intel     | contact@intel.com     | t
  (3 rows)
  </code></pre>
  
  <p>I want my UPSERT statement to look something like this:</p>
  
  <pre><code>INSERT INTO customers (NAME, email)
  VALUES
  ('Microsoft', 'hotline@microsoft.com') 
  ON CONFLICT where (name = 'Microsoft' and active = TRUE)
  DO UPDATE SET email = 'hotline@microsoft.com';
  </code></pre>
  
  <p>The example is a bit contrived but I hope I have been able to communicate the gist here.</p>