Search code examples
postgresqlpostgresql-copy

is there an way to upload 212 columns csv files in PostgreSQL


I have a csv file with 122 columns I am trying this in Postgres. I am trying this

create tble appl_train ();
\copy appl_train FROM '/path/ to /file' DELIMITER ',' CSV HEADER;

I get this error

ERROR:  extra data after last expected column
CONTEXT:  COPY application_train, line 2: "0,100001,Cash loans,F,N,Y,0,135000.0,568800.0,20560.5,450000.0,Unaccompanied,Working,Higher educatio..."

Solution

  • The error message means that the number of columns of your table is less then the number of columns of your csv files.

    If the DDL of your table is exactly what you reported, you created a table with no columns. You have to enumerate (at least) all column name and column data type while creating a table, as reported from documentation:

    CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [
      { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
        | table_constraint
        | LIKE parent_table [ like_option ... ] }
        [, ... ]
    ] )
    [ INHERITS ( parent_table [, ... ] ) ]
    [ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
    [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
    [ TABLESPACE tablespace ]
    
    

    In your code you should have something like this:

    create table appl_train (
      first_column_name integer,
      second_column_name integer,
      third_column_name character varying (20),
    
    // more fields here
    )