ERROR: extra data after last expected column on postgres - postgresql

When I tried to copy a very large txt file into my postgres database, I got a following error below.
Note that I created a table with a single column and am not using any delimiter when importing the txt file.
db1=# create table travis_2018_data (v text);
db1=# \COPY travis_2018_data FROM 'C:\Users\testu\Downloads\travis_2018\2018-Certification\PROP.txt';
The error:
ERROR: extra data after last expected column
CONTEXT: COPY travis_2018_data, line 295032: "000000561125P 02018000000000000
I'm wondering why I still get the error about the extra data (or column) on line 295032 ?

Your text probably contains a tab character which is the default column delimiter for the TEXT format when using \copy (or copy) without specifying a format.
So \copy thinks the line contains two column but only expects one, hence the error message
You need to specify a delimiter that will not occur in the file. The character with the ASCII value 1 is a highly unlikely to occur in such a file, so you can try:
\COPY travis_2018_data FROM '.....' DELIMITER E'\x01'

Related

Invalid input syntax for type bigint: in postgresql timescaledb

I want to transfer data from a csv file named TruckFleet_TruckCyclesQualityMatId.csv to timescaledb.
The name of my db is cycles and the table's name is cycles_table
The detailed view of my table is as follows:
when I try to insert data from the csv file to this table I am getting the
ERROR: Invalid input syntax for type bigint: »HAUL_CYCLE_REC_IDENT«
CONTEXT: COPY cycles_table, line 1, column HAUL_CYCLE_REC_IDENT: »HAUL_CYCLE_REC_IDENT«
The HAUL_CYCLE_REC_IDENT column in the csv file is as follows:-
What is the issue in the table's column HAUL_CYCLE_REC_IDENT datatype.?
The first line of your CSV contains the header, so you must instruct copy to ignore it.
Using the old syntax (< v9.0), you would just add HEADER after CSV, though you might want to move to the current syntax (... WITH (FORMAT CSV, HEADER TRUE)).
In your case, the \copy command could look like
psql -U postgres -d cycles -c "\COPY cycles_table FROM C:\Users\DEGEJOS\Downloads\TruckFleet_TruckCyclesQualityMatId.csv WITH (FORMAT CSV, HEADER)" ```

Bug during COPY in Postgres

I have a table named basic_data which contains more than 8 millions rows and I want to copy all this data into a CSV file.
So I use the COPY command like this :
copy basic_data to '/tmp/data_fdw.csv' delimiter ';' null '';
COPY 8792481
This work great but when I want to insert my data into another table with exactly the same schema (but it's a foreign table), I had this following error:
ERROR: value out of range: overflow

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

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","s.jones#acme.com"
"Jarrod","Barkley","206.555.3454","j.barkley#example.com"
"Wendy","Melvin","415.555.2343","wendy#wendyandlisa.com"
"Lisa","Coleman","425.555.7282","lisa#wendyandlisa.com"
"Jesse","Johnson","507.555.7865","j.j#guitar.com"
"Jean-Luc","Martin","212.555.2244","jean-luc.martin#example.com"
…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.
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. …
COPY table_name FROM 'C:\path\file.csv' DELIMITERS ',' CSV header;
This wasn't the OP's problem, but posting because this is one of the top results when I google the error message.
I was trying to import a .csv file with no header. Adding a header to the file and changing COPY ... CSV to COPY ... CSV HEADER in the sql command fixed the problem for me.

Copy table FROM error in PostgreSQL

I defined a new datatype, say
CREATE TABLE IF NOT EXISTS face_axis ( face INTEGER, normal real[]);
where normal should be a vector.
I have already write them on harddisk, like
5, 1,0,0,
....
Then I want to use
COPY face_axis FROM 'face_axis.csv' with csv;
but it reports error
ERROR: extra data after last expected column
what's wrong with it? thanks,
There are two problems:
The array contents must be enclosed in brackets in all cases
Ambiguous use of comma as both the CSV delimiter and delimiter of values inside the array.
You may use a different CSV separator or quote the contents according to CSV rules:
To be loaded with COPY table FROM file with csv delimiter ';'
1;{1.0,2.0}
To be loaded with COPY table FROM file with csv
1,"{1.0,2.0}"

COPY command does not ignore sequence column

I have a database whereas the first column is labeled as serial not null primary key. The table creation and automatic sequence table creation is successful. However, whenever I do:
copy <table_name> from '/path/to/file' delimiter ',' CSV HEADER;
PostgreSQL tries to read my first column into the serial column, which fails because my first column in my CSV file contains characters (not an integer).
How can I tell the COPY command to populate using a serial column as the first column?
I determined that if I specified the header names and named my columns exactly like the header names in my CSV file, that the import worked:
copy <table_name>(column1, column2, etc) from '/path/to/file' delimiter ',' CSV HEADER;