Python - Import CSV into PostgreSQL Last Updated : 23 Sep, 2021 Comments Improve Suggest changes Like Article Like Report 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 below, we created a table by executing the "create table" SQL command using the cursor.execute() method. '''CREATE TABLE DETAILS(employee_id int NOT NULL, employee_name char(20), employee_email varchar(30), employee_salary float);''' View of the empty table: Table Description After creating the table we need to execute the "copy" command in the SQL form. in the copy command, we need to specify the path of the CSV file. CSV File Used: CSV File Used'''COPY table_name FROM 'C:\folder\file.csv' DELIMITER ',' CSV HEADER;''' Below is the implementation: Python3 import psycopg2 conn = psycopg2.connect(database="EMPLOYEE_DATABASE", user='postgres', password='pass', host='127.0.0.1', port='5432' ) conn.autocommit = True cursor = conn.cursor() sql = '''CREATE TABLE DETAILS(employee_id int NOT NULL,\ employee_name char(20),\ employee_email varchar(30), employee_salary float);''' cursor.execute(sql) sql2 = '''COPY details(employee_id,employee_name,\ employee_email,employee_salary) FROM '/private/tmp/details.csv' DELIMITER ',' CSV HEADER;''' cursor.execute(sql2) sql3 = '''select * from details;''' cursor.execute(sql3) for i in cursor.fetchall(): print(i) conn.commit() conn.close() Output: (1, 'rajesh ', '[email protected]', 60000.0) (2, 'pratyusha ', '[email protected]', 75000.0) (3, 'pratibha ', '[email protected]', 65000.0) Comment More infoAdvertise with us Next Article Python - Import CSV into PostgreSQL isitapol2002 Follow Improve Article Tags : Python Python PostgreSQL Python Pyscopg2 Practice Tags : python Similar Reads 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 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 Perform PostgreSQL CRUD operations from Python The DDL is comprised of the Create, Read, Update, Delete (CRUD) operations which form the backbone of any SQL database system. Let us discuss how to perform CRUD operations on a PostgreSQL database using python. Â Pyscopg2 is the most preferred mode and is widely used to connect the PostgreSQL databa 8 min read Python PostgreSQL - Delete Data In this article, we are going to see how to delete data in tables from PostgreSQL using pyscopg2 module in Python. In PostgreSQL, DELETE TABLE is used to delete the data in the existing table from the database. It removes table definition and all associated data, indexes, rules, triggers, and constr 2 min read 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 Like