Python PostgreSQL - Update Table
Last Updated :
07 Dec, 2021
In this article, we will see how to Update data in PostgreSQL using python and Psycopg2. The update command is used to modify the existing record in the table. By default whole records of the specific attribute are modified, but to modify some particular row, we need to use the where clause along with the update clause.
Syntax for Update Clause
UPDATE table_name SET column1=value1,column2=value2,...
Table for demonstration:

Example 1: Update the columns of the table using Python - pscopg2
Here we are going to see how to update the columns of the table. The table after modification looks like the table shown below. As we can see for every tuple state value is changed to Kashmir.
Python3
# importing psycopg2 module
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="postgres",
user='postgres',
password='password',
host='localhost',
port= '5432'
)
# creating a cursor object
cursor = conn.cursor()
# query to update table with where clause
sql='''update Geeks set state='Kashmir'; '''
# execute the query
cursor.execute(sql)
print('table updated..')
print('table after updation...')
sql2='''select * from Geeks;'''
cursor.execute(sql2);
# print table after modification
print(cursor.fetchall())
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()# code
Output
table updated..
table after updation...
[(1,'Babita','kashmir'),(2,'Anushka','Kashmir'),(3,'Anamika','Kashmir'),
(4,'Sanaya','Kashmir'),(5,'Radha','Kashmir')]
Example 2: Update the columns using the where clause
Here we will use the where clause along with the update table.
Syntax: UPDATE Geeks set state='Delhi' where id=2;
We can see state of the row having id 2 is changed from Hyderabad to Delhi.
Python3
# importing psycopg2 module
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="postgres",
user='postgres',
password='password',
host='localhost',
port= '5432'
)
# create a cursor object
cursor = conn.cursor()
# query to update table
sql='''update Geeks set state='Delhi' where id='2'; '''
# execute the query
cursor.execute(sql)
print("Table updated..")
print('Table after updation...')
# query to display Geeks table
sql2='select * from Geeks;'
# execute query
cursor.execute(sql2);
# fetching all details
print(cursor.fetchall());
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Output:
Table updated..
Table after updation...
[(1, 'Babita', 'Bihar'), (3, 'Anamika', 'Banglore'),
(4, 'Sanaya', 'Pune'), (5, 'Radha', 'Chandigarh'),
(2, 'Anushka', 'Delhi')]
Similar Reads
PostgreSQL Python - Update Data in Table In this article, we are going to see how to update existing data in PostgreSQL tables using the pyscopg2 module in Python. In PostgreSQL, the UPDATE TABLE with where clause is used to update the data in the existing table from the database. Syntax: UPDATE <table_name> SET column1 = value1, c
2 min read
Python PostgreSQL - Drop Table In this article, we are going to see how to drop tables in PostgreSQL using pyscopg2 module Python. In PostgreSQL DROP TABLE is used to remove the existing table from the database. It removes table definition and all associated data, indexes, rules, triggers, and constraints for that table. If the p
2 min read
PostgreSQL - Create table using Python Creating tables in a PostgreSQL database using Python is a common task for developers working with databases. This process involves defining the structure of your data and ensuring that your database is optimized for efficient storage and retrieval. In this article, we will walk through the steps of
3 min read
PostgreSQL UPDATE Statement The PostgreSQL UPDATE statement is an important SQL command used to modify existing data in one or more rows of a table. It allows users to update specific columns or multiple columns at once, using conditions defined in the WHERE clause. This command is highly flexible, enabling dynamic data manage
5 min read
PostgreSQL - Create Tables in Python Creating tables in PostgreSQL using Python is an essential skill for developers working with databases. This article will explore the process of creating new tables in the PostgreSQL database using Python.Why Create PostgreSQL Tables with Python?Using Python to create PostgreSQL tables is beneficial
4 min read
Python PostgreSQL - Select Data In this article, we are going to see how to use select data using Python in PostgreSQL and psycopg2. Installation Open the command prompt and write the command given below. pip install psycopg2 SELECT statement is used to retrieve the required details of an existing table in PostgreSQL. The data tha
3 min read
Python MySQL - Update Query A connector is employed when we have to use MySQL with other programming languages. The work of MySQL-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. Update Clause The update is used to ch
2 min read
Python PostgreSQL - Where Clause In this article, we are going to see how to use the Where clause in PostgreSQL using Psycopg2 in Python. Where Clauses help us to easily deal with the databases. As we know we have a huge amount of data stored in our database, so extracting only useful and required information clauses is helpful. Th
2 min read
PostgreSQL Python - Querying Data Psycopg2 acts as a bridge between Python applications and PostgreSQL databases. Widely employed in diverse Python systems, from web applications to data analysis tools and other software projects, Psycopg2 enables developers to execute queries and manipulate data stored in PostgreSQL databases. In t
5 min read
Python SQLite - Update Data In this article, we will discuss how we can update data in tables in the SQLite database using Python - sqlite3 module. The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using UPDATE statement as per
7 min read