Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG COPY error: 'invalid input syntax for integer' when importing quoted CSV file without any integers

Tags:

csv

postgresql

When trying to use the COPY command via SQL in Postgres 9.5.1 in a simple example database…

I am getting this error:

ERROR:  invalid input syntax for integer: "Sally"
CONTEXT:  COPY customer_, line 2, column id_: "Sally"

********** Error **********

ERROR: invalid input syntax for integer: "Sally"
SQL state: 22P02
Context: COPY customer_, line 2, column id_: "Sally"

…when importing this data in CSV (comma-separated value):

"first_name_","last_name_","phone_","email_"
"Sally","Jones","425.555.1324","[email protected]"
"Jarrod","Barkley","206.555.3454","[email protected]"
"Wendy","Melvin","415.555.2343","[email protected]"
"Lisa","Coleman","425.555.7282","[email protected]"
"Jesse","Johnson","507.555.7865","[email protected]"
"Jean-Luc","Martin","212.555.2244","[email protected]"

…being imported via the following SQL executed in pgAdmin:

COPY customer_
FROM '/home/parallels/Downloads/customer_.csv'
CSV 
HEADER
;

…into this table:

-- Table: public.customer_

-- DROP TABLE public.customer_;

CREATE TABLE public.customer_
(
  id_ integer NOT NULL DEFAULT nextval('customer__id__seq'::regclass),
  first_name_ text NOT NULL,
  last_name_ text NOT NULL,
  phone_ text NOT NULL DEFAULT ''::text,
  email_ text NOT NULL DEFAULT ''::text,
  CONSTRAINT pkey_customer_ PRIMARY KEY (id_)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.customer_
  OWNER TO postgres;
COMMENT ON TABLE public.customer_
  IS 'Represents a person  whose pets visit our clinic.';

So it seems the first row containing the names of the columns is being processed successfully. The failure point is with the first data value in the first data line of the CSV. None of my imported data is of integer type, so the I am befuddled by the error message. The only integer is the id_ primary key, auto-incrementing SERIAL.

I did read the Question page on PG COPY error: invalid input syntax for integer. But that question did involve integer values, and the lack thereof in an empty quoted string being interpreted as a NULL. In my case here we have no integer values in the data; the only integer is the primary key SERIAL column with a DEFAULT generated value (not in the data being imported).

I also found the Question, PostgreSQL ERROR: invalid input syntax for integer. But it seems irrelevant.

like image 865
Basil Bourque Avatar asked Apr 02 '16 02:04

Basil Bourque


2 Answers

Try specifying the columns . . . without the primary key:

COPY customer_ (first_name_ text, last_name_ text, phone_ text, email_ text)
FROM '/home/parallels/Downloads/customer_.csv'
CSV 
HEADER
;

Without the column list, it is looking for a value for id_.

The import data file’s first row of column names are not used for mapping to the table columns. The HEADER flag merely tells Postgres to skip over that first line, as documented:

HEADER

Specifies that… on input, the first line is ignored. …

like image 129
Gordon Linoff Avatar answered Nov 08 '22 16:11

Gordon Linoff


COPY table_name FROM 'C:\path\file.csv' DELIMITERS ',' CSV header;
like image 3
Amar Avatar answered Nov 08 '22 17:11

Amar