使用 pgAdmin 4 导入 CSV 文件 PostgreSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49844570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing CSV file PostgreSQL using pgAdmin 4
提问by Slotmachine
I'm trying to import a CSV file to my PostgreSQL but I get this error
我正在尝试将 CSV 文件导入我的 PostgreSQL,但出现此错误
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
我的 csv 文件很简单
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;
回答by Murtuza Z
回答by kumar kundan
You should try this it must work
你应该试试这个它必须工作
COPY kordinater.test(id,date,time,latitude,longitude) FROM 'C:\tmp\yourfile.csv' DELIMITER ',' CSV HEADER;
COPY kordinater.test(id,date,time,latitude,longitude) FROM 'C:\tmp\yourfile.csv' DELIMITER ',' CSV HEADER;
Your csv header must be separated by commaNOT WITH semi-colon or try to change id column type to bigint
您的 csv 标头必须用逗号分隔,不带分号或尝试将 id 列类型更改为 bigint
回答by Jim Jones
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 tablewith the problematic columns as text
:
使用有问题的列创建一个类似的临时表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
以下命令导入您的文件:
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:
将数据放入tmp
表中后,您可以test
使用以下命令投射坐标并将它们插入表中:
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.
由于您正在使用地理坐标,因此我真诚地建议您查看PostGIS。当您开始使用地理空间数据进行第一次计算时,它非常易于安装并且使您的生活变得更加轻松。