{"id":2658,"date":"2026-04-11T14:56:39","date_gmt":"2026-04-11T07:56:39","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=2658"},"modified":"2026-04-11T15:10:14","modified_gmt":"2026-04-11T08:10:14","slug":"update","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/","title":{"rendered":"PostgreSQL Python: Update Data in a Table with psycopg3"},"content":{"rendered":"\n<p><strong>Summary:<\/strong>&nbsp;In this tutorial, you&#8217;ll learn how to update one or more rows and return updated rows from a table using&nbsp;<strong>Psycopg 3<\/strong>.<\/p>\n\n\n\n<p>The basic steps for updating data in a PostgreSQL table from Python are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, connect to PostgreSQL from Python.<\/li>\n\n\n\n<li>Next, create a&nbsp;<code>Cursor<\/code>&nbsp;object from the&nbsp;<code>Connection<\/code>&nbsp;object.<\/li>\n\n\n\n<li>Then, execute the&nbsp;<code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-update\/\" type=\"page\" id=\"146\">UPDATE<\/a><\/code>&nbsp;statement with values by calling the&nbsp;<code>execute()<\/code>&nbsp;or&nbsp;<code>executemany()<\/code>&nbsp;method of the Cursor object<\/li>\n\n\n\n<li>After that, save the changes automatically when the&nbsp;<code>with<\/code>&nbsp;block completes successfully.<\/li>\n\n\n\n<li>Finally, optionally retrieve the updated row using the&nbsp;<code>RETURNING<\/code>&nbsp;clause and&nbsp;<code>fetchone()<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\" id='prerequisites'>Prerequisites <a href=\"#prerequisites\" class=\"anchor\" id=\"prerequisites\" title=\"Anchor for Prerequisites\">#<\/a><\/h2>\n\n\n\n<p>This tutorial assumes you already have a&nbsp;<code>products<\/code>&nbsp;table like this in the&nbsp;<code>pyinventory<\/code>&nbsp;database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> products (\n    product_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    price <span class=\"hljs-built_in\">NUMERIC<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    safety_stock <span class=\"hljs-built_in\">INTEGER<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    gross_weight <span class=\"hljs-built_in\">NUMERIC<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>)\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We also assume the table already contains some data. If not, you can follow this tutorial to <a href=\"https:\/\/www.pgtutorial.com\/postgresql-python\/insert\/\" type=\"page\" id=\"2650\">insert some data into the table<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"update-one-row-in-a-table\" id='update-one-row-in-a-table'>Update one row in a table <a href=\"#update-one-row-in-a-table\" class=\"anchor\" id=\"update-one-row-in-a-table\" title=\"Anchor for Update one row in a table\">#<\/a><\/h2>\n\n\n\n<p>The following script updates the price and safety stock of one product:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> psycopg\n<span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> config\n\nsql = <span class=\"hljs-string\">\"\"\"\nUPDATE products\nSET price = %s, safety_stock = %s\nWHERE product_id = %s\n\"\"\"<\/span>\n\ndata = (<span class=\"hljs-number\">1099.99<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">1<\/span>)\n\n<span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-keyword\">with<\/span> psycopg.connect(**config) <span class=\"hljs-keyword\">as<\/span> conn:\n        <span class=\"hljs-keyword\">with<\/span> conn.cursor() <span class=\"hljs-keyword\">as<\/span> cur:\n            cur.execute(sql, data)\n            print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{cur.rowcount}<\/span> row updated successfully.\"<\/span>)\n<span class=\"hljs-keyword\">except<\/span> Exception <span class=\"hljs-keyword\">as<\/span> e:\n    print(<span class=\"hljs-string\">f\"Error: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works<\/p>\n\n\n\n<p><strong>Step 1: Import the&nbsp;<code>psycopg<\/code>&nbsp;library<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> psycopg<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The&nbsp;<code>psycopg<\/code>&nbsp;module lets Python connect to PostgreSQL, execute SQL statements, and retrieve result.<\/p>\n\n\n\n<p><strong>Step 2: Import the database configuration<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> config<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This imports the connection settings stored in&nbsp;<code>config.py<\/code>&nbsp;file as a dictionary like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">{\n    <span class=\"hljs-string\">\"host\"<\/span>: <span class=\"hljs-string\">\"localhost\"<\/span>,\n    <span class=\"hljs-string\">\"dbname\"<\/span>: <span class=\"hljs-string\">\"pyinventory\"<\/span>,\n    <span class=\"hljs-string\">\"user\"<\/span>: <span class=\"hljs-string\">\"postgres\"<\/span>,\n    <span class=\"hljs-string\">\"password\"<\/span>: <span class=\"hljs-string\">\"\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Step 3: Define the SQL&nbsp;<code>UPDATE<\/code>&nbsp;statement<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">sql = <span class=\"hljs-string\">\"\"\"\nUPDATE products\nSET price = %s, safety_stock = %s\nWHERE product_id = %s\n\"\"\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This SQL statement updates values in the&nbsp;<code>products<\/code>&nbsp;table:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>UPDATE products<\/code>&nbsp;selects the table&nbsp;<code>products<\/code>&nbsp;to update.<\/li>\n\n\n\n<li><code>SET price = %s, safety_stock = %s<\/code>&nbsp;specifies the columns (price and safety stock) to change.<\/li>\n\n\n\n<li><code>WHERE product_id = %s<\/code>&nbsp;identifies which row to update.<\/li>\n<\/ul>\n\n\n\n<p>The&nbsp;<code>%s<\/code>&nbsp;placeholders let&nbsp;<code>psycopg<\/code>&nbsp;safely bind values in Python to SQL parameters.<\/p>\n\n\n\n<p><strong>Step 4: Prepare the data<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">data = (<span class=\"hljs-number\">1099.99<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This tuple matches the placeholders in the&nbsp;<code>UPDATE<\/code>&nbsp;statement in the following order:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>price<\/code>&nbsp;\u2192&nbsp;<code>1099.99<\/code><\/li>\n\n\n\n<li><code>safety_stock<\/code>&nbsp;\u2192&nbsp;<code>12<\/code><\/li>\n\n\n\n<li><code>product_id<\/code>&nbsp;\u2192&nbsp;<code>1<\/code><\/li>\n<\/ol>\n\n\n\n<p>So this updates the product whose&nbsp;<code>product_id<\/code>&nbsp;is&nbsp;<code>1<\/code>.<\/p>\n\n\n\n<p><strong>Step 5: Start error handling<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">try<\/span>:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We use a&nbsp;<code>try<\/code>&nbsp;block to catch any database errors.<\/p>\n\n\n\n<p><strong>Step 6: Open a database connection<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> psycopg.connect(**config) <span class=\"hljs-keyword\">as<\/span> conn:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This creates a connection to PostgreSQL using the values in the&nbsp;<code>config<\/code>&nbsp;dictionary.<\/p>\n\n\n\n<p>The&nbsp;<code>with<\/code>&nbsp;statement makes resource handling easier by:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Closing the database connection automatically.<\/li>\n\n\n\n<li>Committing the transaction if everything succeeds.<\/li>\n\n\n\n<li>Rolling back the transaction if an error occurs.<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 7: Create a cursor<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> conn.cursor() <span class=\"hljs-keyword\">as<\/span> cur:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We create a cursor to execute the <code>UPDATE<\/code> statement.<\/p>\n\n\n\n<p><strong>Step 8: Execute the&nbsp;<code>UPDATE<\/code>&nbsp;statement<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">cur.execute(sql, data)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The execute() method sends the <code>UPDATE<\/code> statement and parameters to PostgreSQL.<\/p>\n\n\n\n<p>Psycopg safely replaces the placeholders with the values in&nbsp;<code>data<\/code>.<\/p>\n\n\n\n<p><strong>Step 9: Check how many rows were updated<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{cur.rowcount}<\/span> row updated successfully.\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The&nbsp;<code>rowcount<\/code>&nbsp;property of the cursor object stores the number of rows affected by the&nbsp;<code>UPDATE<\/code>&nbsp;statement.<\/p>\n\n\n\n<p><strong>Step 10: Handle errors<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">except<\/span> Exception <span class=\"hljs-keyword\">as<\/span> e:\n    print(<span class=\"hljs-string\">f\"Error: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If an error occurs, Python jumps to the&nbsp;<code>except<\/code>&nbsp;block and displays the error message instead of crashing the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"update-multiple-rows-in-a-table\" id='update-multiple-rows-in-a-table'>Update multiple rows in a table <a href=\"#update-multiple-rows-in-a-table\" class=\"anchor\" id=\"update-multiple-rows-in-a-table\" title=\"Anchor for Update multiple rows in a table\">#<\/a><\/h2>\n\n\n\n<p>The following script updates multiple products using&nbsp;<code>executemany()<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> psycopg\n<span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> config\n\nsql = <span class=\"hljs-string\">\"\"\"\nUPDATE products\nSET price = %s, safety_stock = %s\nWHERE product_id = %s\n\"\"\"<\/span>\n\nproducts = &#91;\n    (<span class=\"hljs-number\">1149.99<\/span>, <span class=\"hljs-number\">14<\/span>, <span class=\"hljs-number\">1<\/span>),\n    (<span class=\"hljs-number\">949.99<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">2<\/span>),\n]\n\n<span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-keyword\">with<\/span> psycopg.connect(**config) <span class=\"hljs-keyword\">as<\/span> conn:\n        <span class=\"hljs-keyword\">with<\/span> conn.cursor() <span class=\"hljs-keyword\">as<\/span> cur:\n            cur.executemany(sql, products)\n            print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{cur.rowcount}<\/span> rows updated successfully.\"<\/span>)\n<span class=\"hljs-keyword\">except<\/span> Exception <span class=\"hljs-keyword\">as<\/span> e:\n    print(<span class=\"hljs-string\">f\"Error: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works<\/p>\n\n\n\n<p><strong>Step 1. Import required modules<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> psycopg\n<span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> config<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Step 2. Define the SQL&nbsp;<code>UPDATE<\/code>&nbsp;statement<\/strong><\/p>\n\n\n\n<p>This UPDATE statement modifies the&nbsp;<code>price<\/code>&nbsp;and&nbsp;<code>safety_stock<\/code>&nbsp;columns for a product identified by&nbsp;<code>product_id<\/code>.<\/p>\n\n\n\n<p><strong>Step 3. Prepare multiple rows of data<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">products = &#91;\n    (<span class=\"hljs-number\">1149.99<\/span>, <span class=\"hljs-number\">14<\/span>, <span class=\"hljs-number\">1<\/span>),\n    (<span class=\"hljs-number\">949.99<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">2<\/span>),\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The products variable stores a list of tuples representing rows to update. Each tuple represents one row:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>first value \u2192 new&nbsp;<code>price<\/code><\/li>\n\n\n\n<li>second value \u2192 new&nbsp;<code>safety_stock<\/code><\/li>\n\n\n\n<li>third value \u2192 target&nbsp;<code>product_id<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Step 4. Open the database connection<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> psycopg.connect(**config) <span class=\"hljs-keyword\">as<\/span> conn:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The&nbsp;<code>connect()<\/code>&nbsp;method creates a PostgreSQL connection. The&nbsp;<code>with<\/code>&nbsp;statement automatically handles commit or rollback.<\/p>\n\n\n\n<p><strong>Step 5. Create a cursor<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> conn.cursor() <span class=\"hljs-keyword\">as<\/span> cur:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This creates a cursor that executes the UPDATE statement.<\/p>\n\n\n\n<p><strong>Step 6. Execute multiple updates<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">cur.executemany(sql, products)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The&nbsp;<code>executemany()<\/code>&nbsp;method runs the same&nbsp;<code>UPDATE<\/code>&nbsp;statement once for each tuple in the&nbsp;<code>products<\/code>&nbsp;list.<\/p>\n\n\n\n<p><strong>Step 7. Display the result<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{cur.rowcount}<\/span> rows updated successfully.\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This displays the number of rows affected.<\/p>\n\n\n\n<p><strong>Step 8. Handle errors<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">except<\/span> Exception <span class=\"hljs-keyword\">as<\/span> e:\n    print(<span class=\"hljs-string\">f\"Error: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If an error occurs, we display the message instead of letting the program crash.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"update-a-row-and-return-it\" id='update-a-row-and-return-it'>Update a row and return it <a href=\"#update-a-row-and-return-it\" class=\"anchor\" id=\"update-a-row-and-return-it\" title=\"Anchor for Update a row and return it\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you may want to update a row and immediately get the new values back. In PostgreSQL, you can do that with the&nbsp;<code>RETURNING<\/code>&nbsp;clause.<\/p>\n\n\n\n<p>The following script updates one row and returns the updated record:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> psycopg\n<span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> config\n\nsql = <span class=\"hljs-string\">\"\"\"\nUPDATE products\nSET price = %s, safety_stock = %s\nWHERE product_id = %s\nRETURNING product_id, name, price, safety_stock, gross_weight\n\"\"\"<\/span>\n\ndata = (<span class=\"hljs-number\">1199.99<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">1<\/span>)\n\n<span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-keyword\">with<\/span> psycopg.connect(**config) <span class=\"hljs-keyword\">as<\/span> conn:\n        <span class=\"hljs-keyword\">with<\/span> conn.cursor() <span class=\"hljs-keyword\">as<\/span> cur:\n            cur.execute(sql, data)\n            row = cur.fetchone()\n\n            <span class=\"hljs-keyword\">if<\/span> row:\n                print(<span class=\"hljs-string\">\"Updated row:\"<\/span>)\n                print(row)\n            <span class=\"hljs-keyword\">else<\/span>:\n                print(<span class=\"hljs-string\">\"No row found to update.\"<\/span>)\n<span class=\"hljs-keyword\">except<\/span> Exception <span class=\"hljs-keyword\">as<\/span> e:\n    print(<span class=\"hljs-string\">f\"Error: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Updated row:\n(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'iPhone 14 Pro'<\/span>, Decimal(<span class=\"hljs-string\">'1199.99'<\/span>), <span class=\"hljs-number\">20<\/span>, Decimal(<span class=\"hljs-string\">'0.24'<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works<\/p>\n\n\n\n<p><strong>Step 1. Import the required modules<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> psycopg\n<span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> config<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Step 2. Define the SQL statement<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">sql = <span class=\"hljs-string\">\"\"\"\nUPDATE products\nSET price = %s, safety_stock = %s\nWHERE product_id = %s\nRETURNING product_id, name, price, safety_stock, gross_weight\n\"\"\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This&nbsp;<code>UPDATE<\/code>&nbsp;statement does two things:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Updates the price and safety stock of a product identified by product id.<\/li>\n\n\n\n<li>Returns the updated row immediately<\/li>\n<\/ul>\n\n\n\n<p>The&nbsp;<code>RETURNING<\/code>&nbsp;clause is handy when you want to verify the result of the update.<\/p>\n\n\n\n<p><strong>Step 3. Prepare the input data<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">data = (<span class=\"hljs-number\">1199.99<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This updates:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>price<\/code>&nbsp;to&nbsp;<code>1199.99<\/code><\/li>\n\n\n\n<li><code>safety_stock<\/code>&nbsp;to&nbsp;<code>20<\/code><\/li>\n\n\n\n<li>for the product with&nbsp;<code>product_id = 1<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Step 4. Execute the update<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">cur.execute(sql, data)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Step 5. Fetch the updated row<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">row = cur.fetchone()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the query uses&nbsp;<code>RETURNING<\/code>, PostgreSQL sends the updated row back to Python. We use&nbsp;<code>fetchone()<\/code>&nbsp;method to read the result.<\/p>\n\n\n\n<p><strong>Step 6. Check whether a row was updated<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> row:\n    print(<span class=\"hljs-string\">\"Updated row:\"<\/span>)\n    print(row)\n<span class=\"hljs-keyword\">else<\/span>:\n    print(<span class=\"hljs-string\">\"No row found to update.\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If no row matches the&nbsp;<code>WHERE<\/code>&nbsp;condition,&nbsp;<code>fetchone()<\/code>&nbsp;returns&nbsp;<code>None<\/code>.<\/p>\n\n\n\n<p><strong>Step 7. Handle errors<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">except<\/span> Exception <span class=\"hljs-keyword\">as<\/span> e:\n    print(<span class=\"hljs-string\">f\"Error: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"important-note-about-the-where-clause\" id='important-note-about-thewhereclause'>Important note about the&nbsp;WHERE&nbsp;clause <a href=\"#important-note-about-thewhereclause\" class=\"anchor\" id=\"important-note-about-thewhereclause\" title=\"Anchor for Important note about the&nbsp;&lt;code&gt;WHERE&lt;\/code&gt;&nbsp;clause\">#<\/a><\/h2>\n\n\n\n<p>If you forgot to include a <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where\/\" type=\"page\" id=\"121\">WHERE<\/a><\/code> clause in the <code>UPDATE<\/code> statement, like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">UPDATE products\nSET price = <span class=\"hljs-number\">999.99<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;ll update&nbsp;<strong>every row<\/strong>&nbsp;in the&nbsp;<code>products<\/code>&nbsp;table.<\/p>\n\n\n\n<p>In most cases, you should include a\u00a0<code>WHERE<\/code>\u00a0condition in the\u00a0<code>UPDATE<\/code>\u00a0statement, so you update only the intended rows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"summary\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use&nbsp;<code>psycopg.connect()<\/code>&nbsp;to open a PostgreSQL connection.<\/li>\n\n\n\n<li>Use&nbsp;<code>cursor()<\/code>&nbsp;to execute an&nbsp;<code>UPDATE<\/code>&nbsp;statement.<\/li>\n\n\n\n<li>Use&nbsp;<code>%s<\/code>&nbsp;placeholders and pass values separately to keep your SQL safe.<\/li>\n\n\n\n<li>Use&nbsp;<code>execute()<\/code>&nbsp;to update one row.<\/li>\n\n\n\n<li>Use&nbsp;<code>executemany()<\/code>&nbsp;to update multiple rows.<\/li>\n\n\n\n<li>Use&nbsp;<code>cur.rowcount<\/code>&nbsp;to check how many rows were affected.<\/li>\n\n\n\n<li>Use&nbsp;<code>RETURNING<\/code>&nbsp;with&nbsp;<code>fetchone()<\/code>&nbsp;if you want the updated row back immediately.<\/li>\n\n\n\n<li>Use&nbsp;<code>with<\/code>&nbsp;blocks to manage connections and cursors automatically.<\/li>\n\n\n\n<li>Use&nbsp;<code>try\/except<\/code>&nbsp;to catch errors and make debugging easier.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2658\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Python: Update Data in a Table with psycopg3\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2658\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Python: Update Data in a Table with psycopg3\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary:&nbsp;In this tutorial, you&#8217;ll learn how to update one or more rows and return updated rows from a table using&nbsp;Psycopg 3. The basic steps for updating data in a PostgreSQL table from Python are as follows: Prerequisites # This tutorial assumes you already have a&nbsp;products&nbsp;table like this in the&nbsp;pyinventory&nbsp;database: We also assume the table already [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2630,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2658","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python PostgreSQL: Update Data in a Table with psycopg3<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to update one or more rows and return updated rows from a table using\u00a0Psycopg 3.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python PostgreSQL: Update Data in a Table with psycopg3\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to update one or more rows and return updated rows from a table using\u00a0Psycopg 3.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-11T08:10:14+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-python\\\/update\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-python\\\/update\\\/\",\"name\":\"Python PostgreSQL: Update Data in a Table with psycopg3\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2026-04-11T07:56:39+00:00\",\"dateModified\":\"2026-04-11T08:10:14+00:00\",\"description\":\"In this tutorial, you'll learn how to update one or more rows and return updated rows from a table using\u00a0Psycopg 3.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-python\\\/update\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-python\\\/update\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-python\\\/update\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Python\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Python: Update Data in a Table with psycopg3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python PostgreSQL: Update Data in a Table with psycopg3","description":"In this tutorial, you'll learn how to update one or more rows and return updated rows from a table using\u00a0Psycopg 3.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/","og_locale":"en_US","og_type":"article","og_title":"Python PostgreSQL: Update Data in a Table with psycopg3","og_description":"In this tutorial, you'll learn how to update one or more rows and return updated rows from a table using\u00a0Psycopg 3.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2026-04-11T08:10:14+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/","url":"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/","name":"Python PostgreSQL: Update Data in a Table with psycopg3","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2026-04-11T07:56:39+00:00","dateModified":"2026-04-11T08:10:14+00:00","description":"In this tutorial, you'll learn how to update one or more rows and return updated rows from a table using\u00a0Psycopg 3.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-python\/update\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-python\/update\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Python","item":"https:\/\/www.pgtutorial.com\/postgresql-python\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Python: Update Data in a Table with psycopg3"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=2658"}],"version-history":[{"count":6,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2658\/revisions"}],"predecessor-version":[{"id":2665,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2658\/revisions\/2665"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2630"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=2658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}