PG COPY error: invalid input syntax for integer

前端 未结 12 1775
走了就别回头了
走了就别回头了 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:46

    this ought to work without you modifying the source csv file:

    alter table people alter column age type text;
    copy people from '/tmp/people.csv' with csv;
    
    0 讨论(0)
  • 2020-12-29 01:49

    I got this error when loading '|' separated CSV file although there were no '"' characters in my input file. It turned out that I forgot to specify FORMAT:

    COPY ... FROM ... WITH (FORMAT CSV, DELIMITER '|').

    0 讨论(0)
  • 2020-12-29 01:49
    CREATE TABLE people (
      first_name varchar(20),
      age        integer,
      last_name  varchar(20)
    );
    

    "first_name","age","last_name" Ivan,23,Poupkine Eugene,,Pirogov

    copy people from 'file.csv' with (delimiter ';', null '');

    select * from people;
    

    Just in first column.....

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

    I think it's better to change your csv file like:

    "age","first_name","last_name"
    23,Ivan,Poupkine
    ,Eugene,Pirogov
    

    It's also possible to define your table like

    CREATE TABLE people (
      age        varchar(20),
      first_name varchar(20),
      last_name  varchar(20)
    );
    

    and after copy, you can convert empty strings:

    select nullif(age, '')::int as age, first_name, last_name
    from people
    
    0 讨论(0)
  • 2020-12-29 01:56

    I had this same error on a postgres .sql file with a COPY statement, but my file was tab-separated instead of comma-separated and quoted.

    My mistake was that I eagerly copy/pasted the file contents from github, but in that process all the tabs were converted to spaces, hence the error. I had to download and save the raw file to get a good copy.

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

    Just came across this while looking for a solution and wanted to add I was able to solve the issue by adding the "null" parameter to the copy_from call:

    cur.copy_from(f, tablename, sep=',', null='')
    
    0 讨论(0)
提交回复
热议问题