Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Update multiple rows in same query in PostgreSQL using Pyscopg2-Python

  • Last Updated : 17 Oct, 2021

In this article, we are going to update multiple rows in the same query in PostgreSQL using Pyscopg2in Python.

We can update multiple values at once by using the update clause from PostgreSQL. First, we import the psycopg2 package and establish a connection to a PostgreSQL database using the pyscopg2.connect() method. 

Syntax of update clause:

UPDATE "table"
SET "column_one" = value1, 
"column_two" = value2, 
"column_three" = valueN
WHERE condition;

Database Used

Below is the implementation:


import psycopg2
conn = psycopg2.connect(
      user='postgres', password='pass',
    host='', port='5432'
conn.autocommit = True
cursor = conn.cursor()
sql = ''' update  student_details  set
          cgpa = 9.5 ,
          branch = 'AE'
        where student_name = 'rahul';'''
sql1 = '''select * from student_details;'''
for i in cursor.fetchall():


(12124468, 'arjun', 9.7, '', 'CSE')
(12124469, 'DIYA', 9.4, '', 'CSE')
(12124466, 'sarah', 9.8, '', 'CSE')
(12124470, 'priya', 8.8, '', 'CSE')
(12124467, 'rahul', 9.5, '', 'AE')

Output in PostgreSQL:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!