I have a CSV file with 10 columns. After creating a PostgreSQL table with 4 columns, I want to copy some of 10 columns into the table.
the columns of my CSV table are like:
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
the columns of my PostgreSQL table should be like:
x2 x5 x7 x10
Best Answer
If it is an ad hoc task
Create a temporary table with all the columns in the input file
Copy from the file into it:
Now insert into the definitive table from the temp:
And drop it:
If it is a frequent task
Use the
file_fdw
extension. As superuser:Grant select permission on the table to the user who will read it:
Then whenever necessary read directly from the csv file as if it were a table: