![]() |
VOOZH | about |
In this article, we will learn to read data from a CSV file and insert it into a PostgreSQL database table using Python.
Let's install the necessary Python libraries:
pip install psycopg2 pandasLet's say we have a file called data.csv
name,age,gender
alice,21,female
abul,23,male
pranjali,22,female
priyanka,24,female
suraj,25,male
Now, we will read the data from the CSV file using the pandas library.
Output:
Now, set up the connection to PostgreSQL database using the psycopg2 library.
Make sure to use your own username and password.
Now, the next step is to create a table in the PostgreSQL database to store the CSV data. The table must have all the columns in the CSV data. In this case, our table will have three columns name, age, and gender.
Inserting the data from the CSV file into the PostgreSQL table.
By following the above steps, we can easily read data from our csv file and add to our postgresql database.