Python Psycopg2 - Insert multiple rows with one query
Last Updated :
26 Oct, 2022
This article is about inserting multiple rows in our table of a specified database with one query. There are multiple ways of executing this task, let's see how we can do it from the below approaches.
Method 1: Inserting Values through Naive method
In this method, we import the psycopg2 package and form a connection using the psycopg2.connect() method, we connect to the 'Classroom' database. after forming a connection we create a cursor using the connect().cursor() method, it'll help us fetch rows. after that we execute the insert SQL statement, which is of the form :
insert into table_name ( column1, column2, .... column_n) values (...) (...) (...) ;
The SQL statement is executed by cursor.execute() method. we fetch rows using the cursor.fetchall() method.
CSV Used:

Example:
Python3
# importing packages
import psycopg2
# forming connection
conn = psycopg2.connect(
database="Classroom",
user='postgres',
password='pass',
host='127.0.0.1',
port='5432'
)
conn.autocommit = True
# creating a cursor
cursor = conn.cursor()
# sql statement to be executed
sql = '''insert into classroom(enrollment_no, name , science_marks)
values(12, 'sarah', 90),(13,'Ray',81); '''
# executing the sql statement
cursor.execute(sql)
# select statement to display output
sql1 = '''select * from classroom;'''
# executing sql statement
cursor.execute(sql1)
# fetching rows
for i in cursor.fetchall():
print(i)
# committing changes
conn.commit()
# closing connection
conn.close()
Output:


Output in Python:
(4, 'Linnett', 79)
(5, 'Jayden', 45)
(6, 'Sam', 63)
(7, 'Zooey', 82)
(8, 'Robb', 97)
(9, 'Jon', 38)
(10, 'Sansa', 54)
(11, 'Arya', 78)
(12, 'sarah', 90)
(13, 'Ray', 81)
Method 2: Inserting Values through cursor.mogrify()
The code is the same as the previous example, but the difference is cursor.mogrify() method.
cursor.mogrify() method:
After the arguments have been bound, return a query string. The string returned is the same as what would be sent to the database if you used the execute() method or anything similar. The string that is returned is always a bytes string and this is faster than executemany() method.
Syntax:
cur.mogrify("INSERT INTO test (col) VALUES (%s, %s....)", (val1, val2,...)(......))
cursor.mogrify() returns a bytes string but we want it to be in string format so we just need to decode the result of mogrify back to a string by using the decode('utf-8') hack.
Example:
Python3
# importing packages
import psycopg2
# forming connection
conn = psycopg2.connect(
database="Classroom",
user='postgres',
password='pass',
host='127.0.0.1',
port='5432'
)
conn.autocommit = True
# creating a cursor
cursor = conn.cursor()
# list of rows to be inserted
values = [(14, 'Ian', 78), (15, 'John', 88), (16, 'Peter', 92)]
# cursor.mogrify() to insert multiple values
args = ','.join(cursor.mogrify("(%s,%s,%s)", i).decode('utf-8')
for i in values)
# executing the sql statement
cursor.execute("INSERT INTO classroom VALUES " + (args))
# select statement to display output
sql1 = '''select * from classroom;'''
# executing sql statement
cursor.execute(sql1)
# fetching rows
for i in cursor.fetchall():
print(i)
# committing changes
conn.commit()
# closing connection
conn.close()
Output:


Method 3: Inserting Values through executemany() method
The approach of this example is the same as before but instead of using cursor.mogrify() we use cursor.executemany() method. executemany() is slower compared to mogrify() method.
executemany():
It is used to Apply a database action (query or command) to all parameter tuples or mappings in the vars list sequence. The function is especially useful for database update instructions because it discards any result set produced by the query. The query's parameters are bound using the same principles as the execute() function.
Syntax
executemany(query, variable_list)
Example:
Python3
# importing packages
import psycopg2
# forming connection
conn = psycopg2.connect(
database="Classroom",
user='postgres',
password='pass',
host='127.0.0.1',
port='5432'
)
conn.autocommit = True
# creating a cursor
cursor = conn.cursor()
# list of rows to be inserted
values = [(17, 'rachel', 67), (18, 'ross', 79), (19, 'nick', 95)]
# executing the sql statement
cursor.executemany("INSERT INTO classroom VALUES(%s,%s,%s)", values)
# select statement to display output
sql1 = '''select * from classroom;'''
# executing sql statement
cursor.execute(sql1)
# fetching rows
for i in cursor.fetchall():
print(i)
# committing changes
conn.commit()
# closing connection
conn.close()
Output:


Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read