{"id":2586,"date":"2013-08-01T14:17:22","date_gmt":"2013-08-01T14:17:22","guid":{"rendered":"http:\/\/www.withdata.com\/ad\/?p=2586"},"modified":"2024-11-05T03:49:19","modified_gmt":"2024-11-05T03:49:19","slug":"replace-update-or-insert-a-row-into-postgresql-table","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/postgresql\/replace-update-or-insert-a-row-into-postgresql-table.html","title":{"rendered":"Replace Update or Insert a row Into PostgreSQL Table"},"content":{"rendered":"<p>In Mysql, if you want to either updates or inserts a row in a table, depending if the table already has a row that matches the data, you can use &#8220;ON DUPLICATE KEY UPDATE&#8221;.<\/p>\n<p>How to do it in PostgreSQL?<\/p>\n<p>A way to do an &#8220;UPSERT&#8221; in postgresql is to do two sequential UPDATE\/INSERT statements that are each designed to succeed or have no effect.<\/p>\n<pre>UPDATE table SET field='C', field2='Z' WHERE id=3;\r\nINSERT INTO table (id, field, field2)\r\n       SELECT 3, 'C', 'Z'\r\n       WHERE NOT EXISTS (SELECT 1 FROM table WHERE id=3);<\/pre>\n<p>The UPDATE will succeed if a row with &#8220;id=3&#8221; already exists, otherwise it has no effect.<\/p>\n<p>The INSERT will succeed only if row with &#8220;id=3&#8221; does not already exist.<\/p>\n<p>You can combine these two into a single string and run them both with a single SQL statement execute from your application. Running them together in a single transaction is highly recommended.<\/p>\n<p>With PostgreSQL 9.1 this can be achieved using a writeable CTE:<\/p>\n<pre>WITH new_values (id, field1, field2) as (\r\n  values\r\n     (1, 'A', 'X'),\r\n     (2, 'B', 'Y'),\r\n     (3, 'C', 'Z')\r\n\r\n),\r\nupsert as\r\n(\r\n    update mytable m\r\n        set field1 = nv.field1,\r\n            field2 = nv.field2\r\n    FROM new_values nv\r\n    WHERE m.id = nv.id\r\n    RETURNING m.*\r\n)\r\nINSERT INTO mytable (id, field1, field2)\r\nSELECT id, field1, field2\r\nFROM new_values\r\nWHERE NOT EXISTS (SELECT 1\r\n                  FROM upsert up\r\n                  WHERE up.id = new_values.id)<\/pre>\n<p>PostgreSQL since version 9.5 has UPSERT syntax, with ON CONFLICT clause. with the following syntax (similar to MySQL)<\/p>\n<pre>INSERT INTO the_table (id, column_1, column_2) \r\nVALUES (1, 'A', 'X'), (2, 'B', 'Y'), (3, 'C', 'Z')\r\nON CONFLICT (id) DO UPDATE \r\n  SET column_1 = excluded.column_1, \r\n      column_2 = excluded.column_2;\r\n<\/pre>\n<p>You can see this page for more about PostgreSQL &#8220;Upsert&#8221;: <a href=\"https:\/\/stackoverflow.com\/questions\/1109061\/insert-on-duplicate-update-postgresql\">https:\/\/stackoverflow.com\/questions\/1109061\/insert-on-duplicate-update-postgresql<\/a><\/p>\n<p>See also:<\/p>\n<p><a href=\"https:\/\/www.withdata.com\/blog\/txttopg\/upsert-data-from-csv-file-to-postgresql-without-programming.html\"> Upsert data from CSV to PostgreSQL <\/a><br \/>\n<a href=\"https:\/\/www.withdata.com\/blog\/txttopg\/upsert-data-from-tsv-file-to-postgresql-without-programming.html\"> Upsert data from TSV to PostgreSQL <\/a><br \/>\n<a href=\"https:\/\/www.withdata.com\/blog\/txttopg\/upsert-data-from-txt-file-to-postgresql-without-programming.html\"> Upsert data from TXT to PostgreSQL <\/a><br \/>\n<a href=\"https:\/\/www.withdata.com\/blog\/xlstopg\/upsert-data-from-excel-file-to-postgresql-without-programming.html\"> Upsert data from Excel to PostgreSQL <\/a><br \/>\n<a href=\"https:\/\/www.withdata.com\/blog\/xmltopostgres\/upsert-data-from-xml-file-to-postgresql-without-programming.html\"> Upsert data from XML to PostgreSQL <\/a><br \/>\n<a href=\"https:\/\/www.withdata.com\/blog\/jsontopostgres\/upsert-data-from-json-file-to-postgresql-without-programming.html\"> Upsert data from JSON to PostgreSQL <\/a><br \/>\n<a href=\"https:\/\/www.withdata.com\/blog\/sqltopostgres\/upsert-data-from-sql-file-to-postgresql-without-programming.html\"> Upsert data from SQL file to PostgreSQL <\/a><\/p>\n<p>Replace (Update\/Insert) a row into other DB:<\/p>\n<p>In SQL Server,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/sql-server\/replace-update-or-insert-a-row-into-sql-server-table.html\">https:\/\/www.withdata.com\/blog\/sql-server\/replace-update-or-insert-a-row-into-sql-server-table.html<\/a>\u00a0.<\/p>\n<p>In Oracle,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/oracle\/replace-update-or-insert-a-row-into-oracle-table-merge-into.html\">https:\/\/www.withdata.com\/blog\/oracle\/replace-update-or-insert-a-row-into-oracle-table-merge-into.html<\/a><\/p>\n<p>In DB2,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/db2\/replace-update-or-insert-a-row-into-db2-table-merge-into.html\">https:\/\/www.withdata.com\/blog\/db2\/replace-update-or-insert-a-row-into-db2-table-merge-into.html<\/a><\/p>\n<p>In Sqlite,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/sqlite\/replace-update-or-insert-a-row-into-sqlite-table.html\">https:\/\/www.withdata.com\/blog\/sqlite\/replace-update-or-insert-a-row-into-sqlite-table.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Mysql, if you want to either updates or inserts a row in a table, depending if the table already has a row that matches the data, you can use &#8220;ON DUPLICATE KEY UPDATE&#8221;. How to do it in PostgreSQL? A way to do an &#8220;UPSERT&#8221; in postgresql is to do two sequential UPDATE\/INSERT statements [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[430],"tags":[238,1370,239,428],"class_list":["post-2586","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-merge-into","tag-postgresql","tag-replace-into","tag-upsert"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/2586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=2586"}],"version-history":[{"count":9,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/2586\/revisions"}],"predecessor-version":[{"id":22560,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/2586\/revisions\/22560"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=2586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=2586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=2586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}