Skip to content Skip to sidebar Skip to footer

How Do I Take Only Specified Required Columns From Csv File And Add Into A Table In Postgresql?

Consider I have *----*------*-----------*----------*----------*---------------*----------------* | id | name | addressid | address1 | address2 | attendancekey | attendancedate | *-

Solution 1:

You will first have to COPY the data to a (perhaps UNLOGGED) staging table and populate the tables from there.

Alternatively, you can define a file_fdw foreign table on the CSV file (which has to be located on the database server machine) and use that to fill the tables. This would save you temporarily loading the file into the database.

Solution 2:

You can try this sequentially... 1. First create three csv file from main csv file. a. For student table, column will id and name ; b. For address table, column will addressid and address1 ; c. For attendance table, column will attendancekey and attendancedate;

Then Execute the below query a. copy student(id,name) from 'filelocation studentfilename.csv' DELIMITERS ',' CSV; b. copy address(addressid,address1) from 'filelocation addressfilename.csv' DELIMITERS ',' CSV; c. copy attendance(attendancekey ,attendancedate) from 'filelocation attendancefilename.csv' DELIMITERS ',' CSV;

Solution 3:

You can iterate over the csv using df.iterrows() and for each row you can insert into the table just the key you need.

Post a Comment for "How Do I Take Only Specified Required Columns From Csv File And Add Into A Table In Postgresql?"