PG COPY error: invalid input syntax for integer

前端 未结 12 1774
走了就别回头了
走了就别回头了 2020-12-29 01:02

Running COPY results in ERROR: invalid input syntax for integer: \"\" error message for me. What am I missing?

My /tmp/people.cs

相关标签:
12条回答
  • 2020-12-29 01:32

    Incredibly, my solution to the same error was to just re-arrange the columns. For anyone else doing the above solutions and still not getting past the error.

    I apparently had to arrange the columns in my CSV file to match the same sequence in the table listing in PGADmin.

    0 讨论(0)
  • 2020-12-29 01:33

    All in python (using psycopg2), create the empty table first then use copy_expert to load the csv into it. It should handle for empty values.

    import psycopg2
    conn = psycopg2.connect(host="hosturl", database="db_name", user="username", password="password")
    cur = conn.cursor()
    cur.execute("CREATE TABLE schema.destination_table ("
                "age integer, "
                "first_name varchar(20), "
                "last_name varchar(20)"
                ");")
    
    with open(r'C:/tmp/people.csv', 'r') as f:
        next(f)  # Skip the header row. Or remove this line if csv has no header.
        conn.cursor.copy_expert("""COPY schema.destination_table FROM STDIN WITH (FORMAT CSV)""", f)
    
    0 讨论(0)
  • 2020-12-29 01:35

    ERROR: invalid input syntax for integer: ""

    "" isn't a valid integer. PostgreSQL accepts unquoted blank fields as null by default in CSV, but "" would be like writing:

    SELECT ''::integer;
    

    and fail for the same reason.

    If you want to deal with CSV that has things like quoted empty strings for null integers, you'll need to feed it to PostgreSQL via a pre-processor that can neaten it up a bit. PostgreSQL's CSV input doesn't understand all the weird and wonderful possible abuses of CSV.

    Options include:

    • Loading it in a spreadsheet and exporting sane CSV;
    • Using the Python csv module, Perl Text::CSV, etc to pre-process it;
    • Using Perl/Python/whatever to load the CSV and insert it directly into the DB
    • Using an ETL tool like CloverETL, Talend Studio, or Pentaho Kettle
    0 讨论(0)
  • 2020-12-29 01:38

    Ended up doing this using csvfix:

    csvfix map -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv
    

    In case you know for sure which columns were meant to be integer or float, you can specify just them:

    csvfix map -f 1 -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv
    

    Without specifying the exact columns, one may experience an obvious side-effect, where a blank string will be turned into a string with a 0 character.

    0 讨论(0)
  • 2020-12-29 01:40

    There is a way to solve "", the quoted null string as null in integer column, use FORCE_NULL option :

    \copy table_name FROM 'file.csv' with (FORMAT CSV, FORCE_NULL(column_name));
    

    see postgresql document, https://www.postgresql.org/docs/current/static/sql-copy.html

    0 讨论(0)
  • 2020-12-29 01:44

    Use the below command to copy data from CSV in a single line without casting and changing your datatype. Please replace "NULL" by your string which creating error in copy data

    copy table_name from 'path to csv file' (format csv, null "NULL", DELIMITER ',', HEADER);
    
    0 讨论(0)
提交回复
热议问题