我正在尝试将 CSV 文件导入 PostgreSQL,其中包含名为
location
的列,其中包含格式为 (-longitude,latitude)
的坐标。但是,我收到以下错误:
2:36846: conversion failed: "(-122.763091,49.04676)" to geography (location)
3:37257: conversion failed: "(-123.141289,49.272057)" to geography (location)
4:36495: conversion failed: "(-122.850334,49.189992)" to geography (location)
我已检查以下内容:
location
列中的值格式正确(-longitude,latitude)
。location
列中没有无效或不完整的值。我正在使用 PostgreSQL 14 和 PostGIS 3.2。
示例:
以下是我的 CSV 文件中的数据示例:
-122.763091,49.04676
-123.141289,49.272057
-122.850334,49.189992
我做错了什么?
如果此示例类似于您在 CSV 文件中保存这些地理位置的方式:
id,description,geog
1,"description1","(-122.850334,49.189992)"
然后将第三个字段读取到
geography
类型列将不起作用,因为这不是有效的 geography
常量。这些都不起作用:
id,description,geog
1,"description1","(-122.850334 49.189992)"
1,"description1","-122.850334,49.189992"
1,"description1","-122.850334 49.189992"
这将:
id,description,geog
1,"description1","point(-122.850334 49.189992)"
您可以将列更改为
text
,按原样导入,在前面添加 point
,将逗号 ,
替换为空格
,然后将 alter
类型恢复为 geography(point,4326)
。 db<>fiddle 的演示:
create table my_table (id int, description text, geog geography(Point,4326));
--preparing a test file:
copy (select '1,"abc","(-122.850334,49.189992)"') to '/tmp/my_file.csv';
alter table my_table
alter column geog type text;
copy my_table from '/tmp/my_file.csv' csv delimiter ',' quote '"';
select *,pg_typeof(geog) from my_table;
id | 描述 | 地理 | pg_typeof |
---|---|---|---|
1 | abc | (-122.850334,49.189992) | 文字 |
update my_table
set geog='point'||replace(geog,',',' ')
returning *,pg_typeof(geog);
id | 描述 | 地理 | pg_typeof |
---|---|---|---|
1 | abc | 点(-122.850334 49.189992) | 文字 |
alter table my_table
alter column geog type geography(point,4326)
using geog::geography(point,4326);
select *,pg_typeof(geog),st_astext(geog) from my_table;
id | 描述 | 地理 | pg_typeof | st_astext |
---|---|---|---|---|
1 | abc | 0101000020E6100000522B4CDF6BB65EC0354069A851984840 | 地理 | 点(-122.850334 49.189992) |