Insert Python list into PostgreSQL database
Last Updated :
17 Feb, 2023
In this article, we will discuss how to Insert a Python list into PostgreSQL database using pyscopg2 module.
Psycopg2 is the most popular PostgreSQL adapter for the Python programming language. Psycopg2 is a DB API 2.0 compliant PostgreSQL driver that is actively developed. It is designed for multi-threaded applications and manages its own connection pool. This module can be installed using the given command:
pip install psycopg2
To insert all records the list is traversed and the values are inserted one by one.
Syntax :
list = [(),(),.....,()]
for d in list:
cursor.execute("INSERT into table_name(
column1,column2, column3.....) VALUES (%s, %s, %s,.....)", d)
First import all the required libraries into the working space and establish database connection. Set auto-commit to false and create a cursor object. Now, create a list of data to be inserted into the table. Loop through the list and insert values. Commit and close connection.
Example: Inserting list values to database
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()
# creating table
sql = '''CREATE TABLE employee(
id SERIAL NOT NULL,
name varchar(20) not null,
state varchar(20) not null
)'''
# list that contain records to be inserted into table
data = [('Babita', 'Bihar'), ('Anushka', 'Hyderabad'),
('Anamika', 'Banglore'), ('Sanaya', 'Pune'),
('Radha', 'Chandigarh')]
# inserting record into employee table
for d in data:
cursor.execute("INSERT into employee(name, state) VALUES (%s, %s)", d)
print("List has been inserted to employee table successfully...")
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Output:
List has been inserted into employee table successfully
Example: Check whether data is being shown in the employee table or not.Â
Python3
# importing psycopg2 module
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="postgres",
user='postgres',
password='password',
host='localhost',
port='5432'
)
# creating cursor object
cursor = conn.cursor()
# query to sort table by name
sql2 = 'select * from employee;'
# executing query
cursor.execute(sql2)
# fetching the result
print(cursor.fetchall())
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Output
[(1, 'Babita', 'Bihar'), (2, 'Anushka', 'Hyderabad'), (3, 'Anamika', 'Banglore'), (4, 'Sanaya', 'Pune'), (5, 'Radha', 'Chandigarh')]
Similar Reads
Python | Database management in PostgreSQL PostgreSQL is an open source object-relational database management system. It is well known for its reliability, robustness, and performance. PostgreSQL has a variety of libraries of API (Application programmable interface) that are available for a variety of popular programming languages such as Py
6 min read
Python - Import CSV into PostgreSQL In this article, we will see how to import CSV files into PostgreSQL using the Python package psycopg2. First, we import the psycopg2 package and establish a connection to a PostgreSQL database using the pyscopg2.connect() method. before importing a CSV file we need to create a table. In the example
2 min read
Handling PostgreSQL BLOB data in Python In this article, we will learn how to Handle PostgreSQL BLOB data in Python. BLOB is a Binary large object (BLOB) is a data type that can store any binary data.To store BLOB data in a PostgreSQL database, we need to use the Binary Large Object (BLOB) data type.By using the Binary Large Object (BLOB)
5 min read
Multiple Postgres databases in psycopg2 PostgreSQL is the most powerful open-source object-relational database management system. Psycopg2 is the most popular PostgreSQL database adapter for Python language. It simply allows you to work with multiple databases in the same program at the same time. This indicates that you can easily switch
4 min read
Save a image file on a Postgres database - Python In this article, we are going to see how to save image files on a postgresql database using Python. 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. Data handled in applications c
4 min read