Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

python - 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 |
*----*------*-----------*----------*----------*---------------*----------------*

columns in CSV file and I want only

  1. id, name to be imported in student table and

  2. addressid, address1, address2 to be imported in address table and

  3. attendancekey and attendancedate to be imported in attendance table in database.

What query to be used to import the required columns into the table?

Database Postgresql Version - 12

Python Version - 3.4.4

Psycopg2 Version - 2.7.5

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...