Software⏱️ 2 min read📅 2026-06-03

How to Fix: Insert, on duplicate update in PostgreSQL?

Insert on duplicate update in PostgreSQL using the correct syntax.

Quick Answer: Use the `INSERT ... ON CONFLICT` statement instead of the MySQL equivalent. For example: `INSERT INTO table (id, field, field2) VALUES (1, A, X), (2, B, Y), (3, C, Z) ON CONFLICT (field, field2) DO UPDATE SET field = EXCLUDED.field, field2 = EXCLUDED.field2;

Several months ago, you learned from an answer on Stack Overflow how to perform multiple updates at once in MySQL using the following syntax:

INSERT INTO table (id, field, field2) VALUES (1, A, X), (2, B, Y), (3, C, Z) ON DUPLICATE KEY UPDATE field=VALUES(Col1), field2=VALUES(Col2);

You've now switched over to PostgreSQL and apparently this is not correct. It's referring to all the correct tables so you assume it's a matter of different keywords being used but you're not sure where in the PostgreSQL documentation this is covered.

💡 Why You Are Getting This Error

  • The syntax you're using is specific to MySQL and not PostgreSQL.

✅ Best Solutions to Fix It

Method 1: Using the `INSERT OR REPLACE` Statement

  1. Step 1: Use the `INSERT OR REPLACE` statement instead of `ON DUPLICATE KEY UPDATE`.

Method 2: Using a `UNION` Statement

  1. Step 1: Use a `UNION` statement to combine multiple inserts into one query.

💡 Conclusion

By using the correct syntax and techniques, you can achieve your desired result in PostgreSQL. Make sure to read through the official documentation for more information.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions