PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values

Cover Image for PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

PostgreSQL INSERT ON CONFLICT UPDATE (upsert) - Use All EXCLUDED Values! 🚀

Have you ever wondered if there's a shorter and simpler way to perform an UPSERT operation in PostgreSQL? You know, to effortlessly handle conflict errors when inserting a row and updating it if it already exists? Well, good news! 🎉 We have a neat solution for you!

Understanding the Problem 😕

Let's say you have a table called tablename with columns like id, username, password, level, and email. You want to upsert a row with specific values, but you also want the possible INSERT to be exactly the same as the possible UPDATE.

In PostgreSQL versions 9.5 and above, you can accomplish this by using the ON CONFLICT clause along with the DO UPDATE statement and explicitly setting each column's value using the EXCLUDED keyword. Here's an example:

INSERT INTO tablename (id, username, password, level, email) 
VALUES (1, 'John', 'qwerty', 5, 'john@mail.com') 
ON CONFLICT (id) DO UPDATE SET 
  id=EXCLUDED.id, username=EXCLUDED.username,
  password=EXCLUDED.password, level=EXCLUDED.level, email=EXCLUDED.email;

Easier and Simpler Solution: 🤩

But wait, is there a shorter way to achieve the same result? Fortunately, there is! 🙌 In PostgreSQL, you can leverage the power of the INSERT INTO ... VALUES ... ON CONFLICT DO NOTHING statement to automatically use all the excluded values without explicitly mentioning each column. Here's how it looks:

INSERT INTO tablename (id, username, password, level, email) 
VALUES (1, 'John', 'qwerty', 5, 'john@mail.com') 
ON CONFLICT (id) DO NOTHING;

Wow, that's much more concise and expressive, right? By using DO NOTHING, PostgreSQL will effectively perform an INSERT if there's no conflict or gracefully skip the row if a conflict occurs. This saves you from the hassle of explicitly specifying all the column values in the UPDATE clause.

Comparing with SQLite: 🔄

If you're familiar with SQLite, you might recall using the INSERT OR REPLACE INTO ... syntax for similar upsert operations. However, PostgreSQL follows a slightly different approach. Though the syntax differs, the functionality is essentially the same.

For reference, here's how you would achieve the upsert operation in SQLite:

INSERT OR REPLACE INTO tablename (id, username, password, level, email) 
VALUES (1, 'John', 'qwerty', 5, 'john@mail.com');

Conclusion and Call-to-Action 📝

You've learned a powerful technique to simplify your UPSERT operations in PostgreSQL using the ON CONFLICT DO NOTHING statement. By using this concise approach, you can effortlessly handle conflicts and take advantage of the excluded values without explicitly mentioning each one. 🚀

Now it's your turn to try it out in your own projects and see the magic unfold! Let us know in the comments how this technique has helped you and if you have any other cool tips to share. Happy coding! 💻💡

Check out our blog for more useful PostgreSQL tips and tricks. Don't forget to subscribe to stay updated with the latest articles and join our community of developers! Let's level up our PostgreSQL game together! 🙌


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello