简体   繁体   English

PostgreSQL从STDIN表达式复制

[英]PostgreSQL COPY FROM STDIN Expressions

I am attempting to use COPY FROM STDIN to import data into my table. 我试图使用COPY FROM STDIN将数据导入到我的表中。 One of the columns in my table is of type geometry. 我表中的一列是几何类型。 My command looks something like this... 我的命令看起来像这样......

COPY "WeatherStations" ("Station_ID", "Station_Code", "Station_Name", "Station_Location") FROM stdin;
1       KAVP    WILKES-BARRE    ST_GeomFromText('POINT(41.338055 -75.724166)')
2       KOKV    WINCHESTER      ST_GeomFromText('POINT(39.143333 -78.144444)')
3       KSHD    SHENANDOAH      ST_GeomFromText('POINT(38.263611 -78.896388)')
...

However, I think it is attempting to insert the text "ST_GeomFromText('POINT..." and failing instead of evaluating the expression and inserting the result of the expression. Does anyone know what might be going on here and how I can get the actual geoms inserted? 但是,我认为它试图插入文本“ST_GeomFromText('POINT ...”并且失败而不是评估表达式并插入表达式的结果。有没有人知道这里可能会发生什么以及如何获得插入实际的geoms?

I had a bad time figuring out how to bulk copy/load geometry data into PostGIS using the COPY FROM STDIN command, I couldn't find official documentation on this topic. 我很难弄清楚如何使用COPY FROM STDIN命令将几何数据批量复制/加载到PostGIS中,我找不到关于此主题的官方文档。

Altering the column during the bulk load ( the ALTER TABLE / SET DATA TYPE / USING ) was not an option to me because it is only supported in PostGIS 2.0+ for the Geometry type, nor was acceptable the use of a temporary table . 在批量加载期间更改列( ALTER TABLE / SET DATA TYPE / USING )对我来说不是一个选项,因为它仅在PostGIS 2.0+中支持 Geometry类型,也不能使用临时表

There is indeed a direct way to do it (at least in PostGIS 1.5.2+). 确实有一种直接的方法(至少在PostGIS 1.5.2+中)。 You can simply rewrite the data for your copy statement this way, using a simple WKT (Well-known text) representation for your Geometry data: 您只需使用Geometry数据的简单WKT(已知文本)表示,就可以通过这种方式重写复制语句的数据:

1       KAVP    WILKES-BARRE    POINT(41.338055 -75.724166)
2       KOKV    WINCHESTER      POINT(39.143333 -78.144444)
3       KSHD    SHENANDOAH      POINT(38.263611 -78.896388)

If you have enforced a SRID constraint on the geometry column you'll have to use the following syntax (in this example the SRID is 4326) known as EWKT (Extended Well-Known Text, which is a PostGIS specific format ): 如果您在几何列上强制执行SRID约束,则必须使用以下语法(在此示例中SRID为4326),称为EWKT(扩展的已知文本,这是PostGIS特定格式 ):

1       KAVP    WILKES-BARRE    SRID=4326;POINT(41.338055 -75.724166)
2       KOKV    WINCHESTER      SRID=4326;POINT(39.143333 -78.144444)
3       KSHD    SHENANDOAH      SRID=4326;POINT(38.263611 -78.896388)

Closing note: there must be no space between "POINT" and the opening parenthesis "(", or the COPY will still return error saying your geometry data has an invalid format. 结束注释:“POINT”和左括号“(”之间必须没有空格,否则COPY仍会返回错误,表示您的几何数据格式无效。

You could omit the function wrapping the text, import into a temporary table with text column, and then run INSERT/SELECT into the permanent table with the function doing the conversion in that step. 您可以省略包装文本的函数,导入带有text列的临时表,然后使用在该步骤中执行转换的函数将INSERT / SELECT运行到永久表中。

INSERT INTO "WeatherStations"
  ("Station_ID", "Station_Code", "Station_Name", "Station_Location")
  SELECT "Station_ID", "Station_Code", "Station_Name",
         ST_GeomFromText("Station_Location")
    FROM "TempWeatherStations";

Point's value looks something like this: 0101000020E6100000DA722EC555552B40CDCCCCCCCC0C4840 . Point的值如下所示: 0101000020E6100000DA722EC555552B40CDCCCCCCCC0C4840

I typically keep latitude and longitude columns in my tables and build spatial data with triggers. 我通常会在表格中保留latitudelongitude列,并使用触发器构建空间数据。

I don't know how to copy POINT s from stdin otherwise. 我不知道如何从stdin复制POINT

You will keep all the values in .csv file and try like this: 您将保留.csv文件中的所有值,并尝试这样:

CAT /path/file/demo.csv | psql -u <username> -h <localhost> -d<database> 
-c "COPY "WeatherStations" ("Station_ID", "Station_Code", "Station_Name", 
"Station_Location") FROM stdin;"

This will work. 这会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM