Slotmachine
Slotmachine

Reputation: 31

Importing CSV file PostgreSQL using pgAdmin 4

I'm trying to import a CSV file to my PostgreSQL but I get this error

ERROR:  invalid input syntax for integer: "id;date;time;latitude;longitude"
CONTEXT:  COPY test, line 1, column id: "id;date;time;latitude;longitude"

my csv file is simple

id;date;time;latitude;longitude
12980;2015-10-22;14:13:44.1430000;59,86411203;17,64274849

The table is created with the following code:

CREATE TABLE kordinater.test
(
    id integer NOT NULL,
    date date,
    "time" time without time zone,
    latitude real,
    longitude real
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE kordinater.test
    OWNER to postgres;

Upvotes: 2

Views: 34753

Answers (3)

Murtuza Z
Murtuza Z

Reputation: 6007

You can use Import/Export option for this task.

  1. Right click on your table
  2. Select "Import/Export" option & Click
  3. Provide proper option
  4. Click Ok button

enter image description here

enter image description here

Upvotes: 3

Jim Jones
Jim Jones

Reputation: 19613

I believe the quickest way to overcome this issue is to create an intermediary temporary table, so that you can import your data and cast the coordinates as you please.

Create a similar temporary table with the problematic columns as text:

CREATE TEMPORARY TABLE tmp
(
    id integer,
    date date,
    time time without time zone,
    latitude text,
    longitude text
);

And import your file using COPY:

COPY tmp FROM '/path/to/file.csv' DELIMITER ';' CSV HEADER;

Once you have your data in the tmp table, you can cast the coordinates and insert them into the test table with this command:

INSERT INTO test (id, date, time, latitude, longitude) 
SELECT id, date, time, replace(latitude,',','.')::numeric, replace(longitude,',','.')::numeric from tmp;

One more thing:

Since you're working with geographic coordinates, I sincerely recommend you to take a look at PostGIS. It is quite easy to install and makes your life much easier when you start your first calculations with geospatial data.

Upvotes: 0

k-kundan
k-kundan

Reputation: 46

You should try this it must work

COPY kordinater.test(id,date,time,latitude,longitude) FROM 'C:\tmp\yourfile.csv' DELIMITER ',' CSV HEADER;

Your csv header must be separated by comma NOT WITH semi-colon or try to change id column type to bigint

to know more

Upvotes: 0

Related Questions