PostgreSQL’s UPSERT is an incredibly powerful feature, allowing for the combination of INSERT and UPDATE operations in a single query. This technique streamlines data management by performing an update if a particular row exists or inserting a new row if it doesn’t.

What is the PostgreSQL UPSERT?

The PostgreSQL UPSERT keyword is a potent fusion of the INSERT and UPDATE SQL commands. It provides a simplified method to insert a new row into a table or update an existing row if a conflict occurs. This conflict is typically related to a violation of a unique constraint, such as a primary key.

Syntax of the PostgreSQL UPSERT Command

Understanding the UPSERT command’s syntax is fundamental to mastering its application. The UPSERT function is executed via the INSERT INTO statement combined with the ON CONFLICT DO UPDATE clause, as shown below:

					INSERT INTO table_name(column1, column2, …)
VALUES (value1, value2, …)
ON CONFLICT (conflict_target)
DO UPDATE SET column1 = value1, …



  • table_name is the name of the table you want to insert into.
  • column1, column2, … are the columns in the table that will receive the values.
  • value1, value2, … are the new values to be inserted or used to update the existing row.
  • conflict_target is the column that has the unique or exclusion constraint.

Exploring the UPSERT Command

To truly comprehend the PostgreSQL UPSERT, we must put it into action. For our exploration, we’ll consider a table ‘orders’ with three columns: order_id, product_name, and quantity.

If you want to add a new order into the table, the syntax would look like this:

					INSERT INTO orders(order_id, product_name, quantity)
VALUES (1, 'Product A', 10)
ON CONFLICT (order_id)
DO UPDATE SET product_name = 'Product A', quantity = 10;


This statement attempts to insert a new order into the ‘orders’ table. If an order with the same order_id already exists, it will update the product_name and quantity for that order_id.

The Power of UPSERT

The PostgreSQL UPSERT’s primary strength lies in its ability to streamline code and eliminate unnecessary checks. In the past, achieving a similar result would require checking if a record exists, then deciding whether to update or insert. UPSERT consolidates these actions into a single atomic operation, greatly reducing code complexity and improving efficiency.

Advanced UPSERT Techniques

UPSERT is not limited to basic operations. You can further refine the DO UPDATE clause with a WHERE condition to update specific rows. You can also exclude the update operation using the DO NOTHING clause, which maintains the existing values in the case of a conflict.

Real-World Examples of PostgreSQL UPSERT

To fully appreciate the power of UPSERT, let’s explore a couple of real-world examples.

Example 1: UPSERT Using a Unique Column

Let’s suppose we have a “users” table with columns “id”, “name”, and “email”, where “email” is a unique column.

					INSERT INTO users (name, email)
   ('John Doe', '')
   name = 'John Doe'


Example 2: UPSERT with a Conditional DO UPDATE

With UPSERT, you can also use a condition in the DO UPDATE clause:

					INSERT INTO users (name, email)
   ('Jane Doe', '')
   name =
WHERE = ''


UPSERT Best Practices

  1. Define Clear Conflict Targets: Clearly specify the conflict target to avoid potential confusion and unintended data manipulation.
  2. Use WHERE Wisely: Use the WHERE clause in the DO UPDATE statement only when necessary to prevent unintentional updates.
  3. Utilize DO NOTHING: When you want to ignore conflicts and keep the existing values, use the DO NOTHING clause.

Wrap up

The PostgreSQL UPSERT is a powerful tool for managing your database efficiently and intelligently. By understanding its functions and how to use them, you can reduce code complexity and enhance your SQL operations.

Check how to install PostgreSQL:

Thanks for reading. Happy coding!