Bulk update of rows in Postgres DB using psycopg2
Last Updated :
04 Dec, 2022
PostgreSQL or Postgres is an open-source, relational, SQL complaint, database management system. It allows, the developers, to create complex applications, supporting SQL and JSON querying. It safely stores, and, scales workloads.
Psycopg2 is a driver, that is used, for interacting, with Postgres data, using the Python scripting language. It is, used to perform, CRUD operations on Postgres data.
In this article, we will learn, to update many records, present in Postgres table, using the Python script. Let us understand, the steps involved here -
1) To perform CRUD operations, on Postgres data, using Python code, we need the psycopg2 library. Hence, install it, using the command - 'pip install psycopg2' at the terminal.
2) In the example, we will refer, to the Employee database, having an Employee table. The Employee table, has the fields, Employee Name, Department name, and Coupon Points.
3) The psycopg2 adapter, has 'connect()' method, that allows connection to the database, bypassing the Database Name, username, password, and hostname as parameters. It returns a valid connection object, on a successful connection.
4) Using the connection object, we can create a cursor object, that acts as a pointer to the database. We can use, the cursor object, to fire SQL queries, on the table data. Using it, we will Create and Insert a few records, in the table. The code for the same is as mentioned below -
Python3
#Import the required psycopg2 library
import psycopg2
#Method to create a connection object to the database.
#It creates a pointer cursor to the database and returns it along with Connection object
def create_connection():
#Connect to the Postgresql database using the psycopg2 adapter.
#Pass your database name , username , password , hostname and port number
conn = psycopg2.connect("dbname='EmployeeDB' user='postgres' password='admin' host='localhost' port='5432'")
#Get the cursor object from the connection object
curr = conn.cursor()
return conn,curr
# Method to create a table using the create query.
def create_table():
try:
# Get the cursor object from the connection object
conn, curr = create_connection()
try:
# Fire the CREATE query
curr.execute("CREATE TABLE IF NOT EXISTS employee(employeeName TEXT, departmentName TEXT, couponPoints REAL)")
except(Exception, psycopg2.Error) as error:
# Print exception
print("Error while creating PostgreSQL table", error)
finally:
# Close the connection object
conn.commit()
conn.close()
finally:
#Since we do not have to do anything in this finally block we will pass
pass
# Insert data in the employee table using the INSERT query
def insert_values(employeeName, departmentName, couponPoints):
try:
# Get the cursor object from the connection object
conn, curr = create_connection()
try:
# Fire the INSERT query
curr.execute("INSERT INTO employee(employeeName,departmentName,couponPoints) values(%s,%s,%s)",
(employeeName, departmentName, couponPoints))
except(Exception, psycopg2.Error) as error:
# Print exception
print("Error while inserting PostgreSQL table", error)
finally:
# Close the connection object
conn.commit()
conn.close()
finally:
#Since we do not have to do anything in this finally block we will pass
pass
#Call the create table method
create_table()
#Insert data by calling insert method. One example is shown below.
insert_values("Rohan Shetty","HR","250")
To view the Postgres data, we can use the PgAdmin4 tool. The table data, looks like this, as shown below -
The initial Employee table data as seen using PgAdmin4 tool
5) Now, suppose a requirement comes, wherein, we need to update the coupon points value, of all the employees, according to the Department, in which they are present. Let's assume. the organization decides, to increase the coupon points, of all HR department employees, by 2, Finance department employees, by 3.
6) Here, we can bulk update the points data, based on the department, in which the Employee, is present. The executemany() method, is a useful method, for executing, a database query, bypassing mappings, in the tuple structure.
7) The executemany() method, will take a list of tuples, and, modify the values, which will be reflected, across all employees. The code for the same will look as below -
Python3
#Import the required psycopg2 library
import psycopg2
#Method to create a connection object to the database.
#It creates a pointer cursor to the database and returns it along with Connection object
def create_connection():
#Connect to the Postgresql database using the psycopg2 adapter.
#Pass your database name , username , password , hostname and port number
conn = psycopg2.connect("dbname='EmployeeDB' user='postgres' password='admin' host='localhost' port='5432'")
#Get the cursor object from the connection object
curr = conn.cursor()
return conn,curr
# Method returns a list of all records present in the Employee table.
def select_values():
try:
# Call the create_connection method to get Connection and Cursor objects.
conn, curr = create_connection()
try:
# Use the cursor object to pass the wrapped SQL query
curr.execute("SELECT * FROM employee")
# The fetchall method will get all table rows in a list of tuples
rows = curr.fetchall()
# Display the table data
print(rows)
except(Exception, psycopg2.Error) as error:
# Print the exception in case of any error
print("Error while selecting Employees from table", error)
finally:
# Close the connection object
conn.close()
finally:
#Since we do not have to do anything in this finally block we will pass
pass
# Method used to update the Coupon points based on Department Name.
def updateDeptPoints(deptPoints):
try:
# Call the create_connection method to get Connection and Cursor objects.
conn, curr = create_connection()
try:
# Prepare the update query as per the columns required
deptpoints_update_query = """Update employee set couponPoints = couponPoints * %s where departmentName = %s"""
# Pass the new values and update query to the executemany() method of Cursor
curr.executemany(deptpoints_update_query, deptPoints)
except(Exception, psycopg2.Error) as error:
# Print Exception in case of error
print("Error while updating PostgreSQL table", error)
finally:
# Close the connection object
conn.commit()
conn.close()
finally:
#Since we do not have to do anything in this finally block we will pass
pass
#Prepare, a list, of tuples, of values to be updated
#Since we have to update according to the department we will pass
#the tuple as (correction_value_coupon, department_name)
#Here HR department values are to be increased by 2 and Finance by 3
tuple_dept = [(2,'HR'),(3,'Finance')]
#Call the update method by passing list of updates
updateDeptPoints(tuple_dept)
#Call the select method to display all updated records
select_values()
The output of the same in PyCharm is as shown below -
Output in PyCharm after Bulk update
8) Instead of updating records individually, we have done the operation using a single statement. After updating the records the Employee data, in PgAdmin4, looks as shown below -
Employee data after using executemany() method for updating coupon points
Conclusion: The executemany() method can also be used to insert bulk data in the table. Thus, we have studied how to update data in bulk.
Similar Reads
Do loop in Postgresql Using Psycopg2 Python
In this article, we use psycopg2 to loop through all data points using psycopg2 function in Python. We will first connect our PostgreSQL database using psycopg2.connect method, and pass the connection parameters such as the host, database, user, and password. Then we will create a cursor using the c
4 min read
Update multiple rows in same query in PostgreSQL using Pyscopg2-Python
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 pyscopg
1 min read
Python Select from PostgreSQL Table using Psycopg2
This article will introduce you to the use of the psycopg2 module which is used to connect to a PostgreSQL database from Python. We will look over how to establish a connection to a database, create a cursor object to execute DBMS SQL statements, execute a SELECT statement to retrieve the data from
7 min read
Python PostgreSQL Connection Pooling Using Psycopg2
In this article, We will cover the basics of connection pooling using connection pooling in Python applications, and provide step-by-step instructions on how to implement connection pooling using Psycopg2. Whether you are building a small-scale application or a large-scale enterprise application, un
6 min read
How to Log Queries in PostgreSQL using Python?
Python has various database drivers for PostgreSQL. Current most used version is psycopg2. It fully implements the Python DB-API 2.0 specification. The psycopg2 provides many useful features such as client-side and server-side cursors, asynchronous notification and communication, COPY command suppor
4 min read
Get column names from PostgreSQL table using Psycopg2
This article is an illustration of how to extract column names from PostgreSQL table using psycopg2 and Python. Used table for demonstration: Example 1: First, we connect the PostgreSQL database using psycopg2.connect()Â method, then we create a cursor using cursor() method, after that we use the cu
1 min read
Storing a BLOB in a PostgreSQL Database using Python
This article focuses on, Storing BLOB in a PostgreSQL database. BLOB is a Binary large object (BLOB) is a data type that can store any binary data.To Store Blob data in a Postgres database Table, we will use psycopg2.The table for storing BLOB data in PostgreSQL is called a Large Object table and th
3 min read
How to Update Table Rows in PostgreSQL Using Subquery?
PostgreSQL is a general-purpose object-relational database management system and is open-source. It ensures data integrity features like constraints, transactions and foreign key support. In this article, We will understand How to update table rows in PostgreSQL using subquery using various methods,
5 min read
Python | Getting started with psycopg2-PostGreSQL
PostgreSQL is a powerful, open source object-relational database system. PostgreSQL runs on all major operating systems. PostgreSQL follows ACID property of DataBase system and has the support of triggers, updatable views and materialized views, foreign keys. To connect PostgreSQL we use psycopg2 .
1 min read
Migration of table from CSVto postgres using Python
In this article, we will learn to read data from a CSV file and insert it into a PostgreSQL database table using Python. PrerequisitesPython installed on your machine.PostgreSQL server and database setup.psycopg2 library to connect to PostgreSQL from Python.pandas library to handle CSV data.Let's in
2 min read