From 1740c775da66d9607a223039bce4989d345be0f3 Mon Sep 17 00:00:00 2001 From: Jack Jackson Date: Thu, 26 Dec 2024 16:40:28 -0800 Subject: [PATCH] Upsert-in-postgres --- blog/content/posts/upsert-in-postgres.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 blog/content/posts/upsert-in-postgres.md diff --git a/blog/content/posts/upsert-in-postgres.md b/blog/content/posts/upsert-in-postgres.md new file mode 100644 index 0000000..c1a882e --- /dev/null +++ b/blog/content/posts/upsert-in-postgres.md @@ -0,0 +1,15 @@ +--- +title: "Upsert in Postgres" +date: 2024-12-26T16:35:42-08:00 +tags: + - tech-snippets + +--- +A _real_ quick blog post just to record a useful technique I just discovered that I'll want to have a record for in the future - if inserting into a Postgres table, so long as you're on `>9.5`, you can upsert-and-overwrite with the following syntax: + +```sql +INSERT INTO tablename (a, b, c) values (1, 2, 10) +ON CONFLICT (a) DO UPDATE SET a = EXCLUDED.a, b = EXCLUDED.b, c = EXCLUDED.c; +``` + +Ref [here](https://stackoverflow.com/a/30118648/1040915).