Insert Python list into PostgreSQL database Last Updated : 17 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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')] Comment More infoAdvertise with us Next Article Insert Python list into PostgreSQL database A annulata2402 Follow Improve Article Tags : Python Python PostgreSQL Python Pyscopg2 Practice Tags : python 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 How to List Databases and Tables in PostgreSQL using PSQL PostgreSQL is a powerful, open-source object-relational database system. It provides a wide array of tools and features to manage databases, tables, and other database objects. In this article, we will explain how to list databases and tables in PostgreSQL using the psql command-line interface. We w 3 min read CRUD Operations on Postgres using Async Database In Python CRUD stands for Create, Read, Update and Delete Operations. All these Operations can be made Asynchronous using the Async Database Connection. After making  Async Connection to Postgres Database, the performance of the Application improves significantly as all the operations are performed Concurrent 3 min read PostgreSQL - Connect To PostgreSQL Database Server in Python The psycopg database adapter is used to connect with PostgreSQL database server through python. Installing psycopg: First, use the following command line from the terminal: pip install psycopg If you have downloaded the source package into your computer, you can use the setup.py as follows: python s 4 min read How to Migrate a PostgreSQL Database to MySQL Moving a database from one platform to another can be tough, but with careful planning and execution, it can be done smoothly. In this article, we'll go over how to migrate a PostgreSQL database to MySQL, which are both popular RDBMS. We'll cover preparation, schema conversion, data migration, and t 5 min read PostgreSQL - Restore Database Restoring a PostgreSQL database is an essential process for data recovery, especially when dealing with data loss, corruption, or unintentional errors. The ability to efficiently restore a database ensures that an organization can maintain data integrity, availability, and consistency. In this artic 5 min read Like