PostgreSQL - Insert Data Into a Table using Python Last Updated : 30 Aug, 2020 Comments Improve Suggest changes Like Article Like Report In this article we will look into the process of inserting data into a PostgreSQL Table using Python. To do so follow the below steps: Step 1: Connect to the PostgreSQL database using the connect() method of psycopg2 module.conn = psycopg2.connect(dsn) Step 2: Create a new cursor object by making a call to the cursor() methodcur = conn.cursor() Step 3: Now execute the INSERT statement by running the execute() methodcur.execute(sql, (value1,value2)) Step 4: After inserting the data call the commit() method to make the changes permanent.conn.commit() Step 5: Now terminate the cursor and the connection to the database.cur.close() conn.close() Example: For example we will use the Student table of the school database that we created in the earlier sections of the article series. Here we will create a insert_student() function to insert student_name row to the student table: Python3 #!/usr/bin/python import psycopg2 from config import config def insert_student(student_name): """ insert a new vendor into the vendors table """ sql = """INSERT INTO students(student_name) VALUES(%s) RETURNING student_id;""" conn = None student_id = None try: # read database configuration params = config() # connect to the PostgreSQL database conn = psycopg2.connect(**params) # create a new cursor cur = conn.cursor() # execute the INSERT statement cur.execute(sql, (student_name,)) # get the generated id back student_id = cur.fetchone()[0] # commit the changes to the database conn.commit() # close communication with the database cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() return student_id Now to verify the insertion use the following command in the psql shell: SELECT * FROM student; Output: Comment More infoAdvertise with us Next Article PostgreSQL - Insert Data Into a Table using Python D ddeevviissaavviittaa Follow Improve Article Tags : Python PostgreSQL postgreSQL-managing-table Practice Tags : python Similar Reads 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 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 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 Insert Python list into PostgreSQL database 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- 2 min read Python MariaDB - Insert into Table using PyMySQL MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python. In this article we will look into the process of inserting rows to a table of the database using pymysql. You can insert o 2 min read Get the id after INSERT into MySQL database using Python Prerequisites: MySQL, mysql-connector for python The task here is to draft a Python program that works with SQL support to connect data. Whenever insertion into the database takes place, the ID of the row inserted will be printed. To connect python with the database we are using MySQL connector. The 2 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 Python MySQL - Insert into Table MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify 3 min read PostgreSQL - Connecting to the Database using Python PostgreSQL in Python offers a robust solution for developers looking to interact with databases seamlessly. With the psycopg2 tutorial, we can easily connect Python to PostgreSQL, enabling us to perform various database operations efficiently. In this article, we will walk you through the essential 4 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 Like