【问题标题】:Importing CSV to postgres with new columns使用新列将 CSV 导入到 postgres
【发布时间】:2017-06-29 10:58:55
【问题描述】:

我在将 csv 数据导入到 postgres 数据库时遇到问题,通过我的数据库“地标”上的以下命令启用了地理数据/postgis:

CREATE EXTENSION postgis;

所以....故事是这样的:

我正在关注这个tutorial

我正在尝试使用这些列导入 csv

name    conf    capital venture latitude    longitude

第一行,作为数据的例子,是:

example, 1, 1, 1, 51.51923, -0.12205

我已经按照教程设置了表格,除了添加 conf、capital 和风险而不是他的数据中的列(地址、date_built、架构师、地标)。即:

CREATE TABLE landmarks
(
  gid serial NOT NULL,
  name character varying(50),
  conf character varying(10),
  capital character varying(10),
  venture character varying(10),
  the_geom geometry,
  CONSTRAINT landmarks_pkey PRIMARY KEY (gid),
  CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
  CONSTRAINT enforce_geotype_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL),
  CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326)
);

然后

CREATE INDEX landmarks_the_geom_gist
  ON landmarks
  USING gist
(the_geom );

其他数据与他的example基本相同。

我已经正确设置了表格并启用了 postgis 扩展来处理 geom 数据。

但是,当我尝试导入我的 csv 时,问题就来了:

landmarks=# \copy landmarks(name,conf,capital,venture,latitude,longitude) FROM '../../../../../var/tmp/map2.csv' DELIMITERS ',' CSV HEADER;
ERROR:  column "latitude" of relation "landmarks" does not exist

现在,我注意到当他创建表时,他没有添加纬度或经度列...所以我想知道这是否是问题所在,并尝试使用这些列和整数创建另一个表,但是只是给我这个错误:

ptmap3=# \copy landmarks(name,conf,capital,venture,latitude,longitude) FROM '../../../../../var/tmp/map2.csv' DELIMITERS ',' CSV HEADER;
ERROR:  invalid input syntax for integer: "51.51923"
CONTEXT:  COPY landmarks, line 2, column latitude: "51.51923"

所以...似乎如果我添加纬度列那么它可以工作,但数据失败?使用此检查 csv 是否有错误后

od -c map2.csv

...我的 csv 没有任何问题(没有隐藏字符或错误)...所以有什么问题?

如果有人能帮我将我的 csv 导入到这个数据库中,我将不胜感激!

【问题讨论】:

  • 错误:整数输入语法无效:“51.51923” 将字符串值分配给整数列时会发生此错误。能否请您检查一下 csv 文件中的纬度和经度数据如何
  • 在命令行中使用 od 读取,该值逐字符存储为 , 5 1 。 5 1 9 2 3 ,

标签: postgresql csv postgis


【解决方案1】:

您需要分两个步骤将(latitude,longitude) 数据导入数据库的几何列。

1. 将数据导入latitudelongitude 两个浮点列:

来自您的原始表地标:

CREATE TABLE landmarks
(
  gid serial NOT NULL,
  name character varying(50),
  conf character varying(10),
  capital character varying(10),
  venture character varying(10),
  the_geom geometry,
  CONSTRAINT landmarks_pkey PRIMARY KEY (gid),
  CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
  CONSTRAINT enforce_geotype_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL),
  CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326)
);

CREATE INDEX landmarks_the_geom_gist
  ON landmarks
  USING gist
(the_geom );

添加临时两列latitudelongitude

ALTER TABLE landmarks ADD COLUMN latitude double precision;
ALTER TABLE landmarks ADD COLUMN longitude double precision;

然后导入你的数据:

\copy landmarks(name,conf,capital,venture,latitude,longitude) FROM '../../../../../var/tmp/map2.csv' DELIMITERS ',' CSV HEADER;

2. 通过从latitudelongitude 创建 POINT 几何来填充几何列:

UPDATE landmarks SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);

最后,删除临时列:

ALTER TABLE landmarks DROP COLUMN latitude;
ALTER TABLE landmarks DROP COLUMN longitude;

【讨论】:

    【解决方案2】:

    还有另一种方法。

    1. Google pgfutter 并下载相应版本的可执行文件。 link 适用于 Windows,但也提供其他版本。
    2. 确保pgfutter 文件和yourdata.csv 文件位于同一目录中。
    3. 在终端设置目录并运行以下代码。
    4. 这也会创建一个新表,因此只需输入一个新的表名。

       pgfutter.exe --host "localhost" --port "5432" --db "YourDatabaseName" --schema "public" --table "TableName" --user "postgres" --pw "YourPassword" csv YourData.csv
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-21
      • 2016-02-10
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多