When I try to copy a CSV file with pgAdmin or just COPY
command it shows me this error
ERROR: nieprawidłowa składnia wejścia dla typu integer: "1,"A Dijiang","M",24,180,80,"China","CHN","1992 Summer","1992","Summer","Barcelona","Basketball","Basketball Men's Basketball",NA"
CONTEXT: COPY olympics_history, line 2, column ID: "1,"A Dijiang","M",24,180,80,"China","CHN","1992 Summer","1992","Summer","Barcelona","Basketball","Ba..."
BŁĄD: nieprawidłowa składnia wejścia dla typu integer: "1,"A Dijiang","M",24,180,80,"China","CHN","1992 Summer","1992","Summer","Barcelona","Basketball","Basketball Men's Basketball",NA"
SQL state: 22P02
I don’t know why it’s in Polish, but it just means invalid input syntax for integer type.
Here is table schema (ultra basic but I just need it for training)
Column | Type | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
ID | integer | | |
name | character varying | | |
sex | character varying | | |
age | character varying | | |
height | character varying | | |
weight | character varying | | |
team | character varying | | |
noc | character varying | | |
games | character varying | | |
year | integer | | |
season | character varying | | |
city | character varying | | |
sport | character varying | | |
event | character varying | | |
medal | character varying | | |
In the CSV file everything seems fine; the data is separated by commas etc. I just started learning and have no idea what can be wrong here.
I tried putting every header in "", recreating table.
2
Answers
No the quoting is incorrect: the entire row of data is enclosed by double quotes, which is not permitted. How to quote is explained in RFC 4180, see in particular rules #5 and #7.
The first line instead of being
should be
and whenever double quotes are present in the fields, they must be doubled.
You probably forgot to specify the format when calling
copy
. By default, when you run something like this:COPY
assumes yourFORMAT
istext
, notcsv
, even if you feed it a file with a.csv
extension:Problem with that is default delimiter for
text
is a tab, not a comma, andquote
isn’t supported:My bet is you just need explicitly specify the format, and it’ll work fine:
See this example: