Question

I am trying to import data from csv file to my postgres table using command line, and have tried COPY as well, but every time it give me the following error, can anyone please help me where am I going wrong, as I am first time using Postgres, and after studying about I came to know that I can use COPY command to import and export data into csv and to table.

Below is me table schema where I am trying to import the csv file

create table tdummy2
(
number1 integer,
digit integer,
type_digit integer,
total integer,
word character varying(256),
apk character varying(256),
version1 character varying(256),
created  timestamp without time zone DEFAULT now() NOT NULL,
updated  timestamp without time zone DEFAULT now() NOT NULL
);

and this is how my csv file looks:

number1;"digit";"type_digit";"total";"word";"apk";"version1"
8;88444;34;97;"yt";"kjk";"vrt"
8;88444;34;97;"yt";"kjk";"vrt"
56;88444;34;97;"yt";"kjk";"vrt"
67;88444;34;97;"yt";"kjk";"vrt"

And this is the error I am getting while running the COPY command from command line

dummy_table=# COPY tdummy2 FROM 'C:\Users\user1\Documents\user1\import.csv' DELIMITER ',' CSV;

ERROR:  invalid input syntax for integer: "number1;digit;type_digit;total;word;apk;version1"
CONTEXT:  COPY tdummy2, line 1, column number1: "number1;digit;type_digit;total;word;apk;version1"

dummy_table=#
Was it helpful?

Solution

Your file is semicolon delimited, but you've told PostgreSQL it's comma-delimited.

Try

COPY tdummy2 FROM 'C:\Users\user1\Documents\user1\import.csv' DELIMITER ';' CSV;
                                                                        ^^^
                                                                      semicolon

and see if that helps.

If the CSV has a header, specify the HEADER option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top