How to import CSV file data into PostgreSQL table

In this article, we will see How to import CSV file data into PostgreSQL table. It is nothing but loading data from a spreadsheet.

1. Create table

CREATE TABLE student(sno int primary key, sname varchar(50), sage int, dob date);

2. Create a spreasheet with name student_new.csv. Paste the following data in the CSV file and save it.

sno,sname,sage,dob 1001,"David",9,'11-09-2011' 1002,"Gates",11,'02-01-2009' 1003,"r2schools",13,'12-09-2007' 1004,"karunakar",10,'01-07-2010'

3. Connect to database where the student table is created in PostgreSQL.

\c production

4. Then, run the command

copy student from '/home/r2schools/student_new.csv' DELIMITER ',' CSV HEADER;

5. Now verify the data in the table.

select * from student;

We can also use third party tool to load data from spreedsheet(CSV) to PostgreSQL table.