I'm a little confused about the amount of possibilities to import csv-data into a postgres database.
My issue: I have a csv-file with about 70 columns, but I only want to import specific columns of these 70.
I've tried the psycopg2.copy_from()
, but when I specify my columns like this:
psycopg2.copy_from(csv_in, 'stamm_data', sep=';', columns = 'col_1', 'col_4', 'col_7')
I get:
Error: extra data after last expected column
So, another solution is to work with dictionaries
with the column-name as key, the data as value and then import the data with psycopg2.execute("INSERT INTO..")
into the database.
I also could write my needed columns into a new csv-file and import it with psycopg2.copy_from()
.
Or I use psycopg2.execute("COPY ..")
or psycopg2.copy_expert()
.
So, whats the most effective way for my issue?
['col_1','col_4','col_7']
- and I do not think you can select individual columns like that (from the documentation): "The length and types should match the content of the file to read. If not specified, it is assumed that the entire table matches the file structure." but try with the iterable first. – Burhan Khalid Mar 5 at 8:57columns
option is for enumerating which columns in your already-created database table will receive the data imported from the file. For example, if you are importing this data into a table with 90 columns, you would need this option to specify which 70 are going to be populated. Sincecopy_from
is a wrapper around the native PostgreSQLCOPY
functionality, it probably can't do anything short of importing the entirety of each row it encounters. – rchang Mar 5 at 11:10