【问题标题】:Is it possible to import OSM data from inside psql?是否可以从 psql 中导入 OSM 数据?
【发布时间】:2016-02-23 14:28:06
【问题描述】:

我正在为 Dockerfile 编写这个 Bash 脚本:

#!/bin/bash
set -e

gosu postgres postgres --single -jE <<-EOL
  CREATE USER "$OSM_USER";
EOL

gosu postgres postgres --single -jE <<-EOL
  CREATE DATABASE "$OSM_DB";
EOL

gosu postgres postgres --single -jE <<-EOL
  GRANT ALL ON DATABASE "$OSM_DB" TO "$OSM_USER";
EOL

# Postgis extension cannot be created in single user mode.
# So we will do it the kludge way by starting the server,
# updating the DB, then shutting down the server so the
# rest of the docker-postgres init scripts can finish.

gosu postgres pg_ctl -w start
gosu postgres psql "$OSM_DB" <<-EOL
  CREATE EXTENSION postgis;
  CREATE EXTENSION hstore;
  ALTER TABLE geometry_columns OWNER TO "$OSM_USER";
  ALTER TABLE spatial_ref_sys OWNER TO "$OSM_USER";
EOL
gosu postgres pg_ctl stop

我想在 ALTER TABLE 之后添加两个导入命令:

shp2pgsql -I -s 4326 -W "latin1" post_pl.shp post_pl  > post_pl.sql
psql -h 172.17.0.2 -U postgres -d gis -f post_pl.sql

osm2pgsql -H 172.17.0.2  -U postgres -d gis --hstore -s -S `./osm_stylesheet ./hessen-latest.osm.pbf`

我的问题是,它可以工作吗?我们可以在 psql 中导入数据吗?如果是,我该怎么做?

TNX

安德烈·拉姆尼科夫

【问题讨论】:

    标签: bash postgresql docker psql dockerfile


    【解决方案1】:

    您可以使用\i 将其包含在此处的文档中(您可能需要将反斜杠加倍)假设您的post_pl.sql 在当前目录中(否则请指定完整路径名)

    gosu postgres pg_ctl -w start
    gosu postgres psql "$OSM_DB" <<-EOL
      CREATE EXTENSION postgis;
      CREATE EXTENSION hstore;
      ALTER TABLE geometry_columns OWNER TO "$OSM_USER";
      ALTER TABLE spatial_ref_sys OWNER TO "$OSM_USER";
      \\connect gis -- Assuming a different DB is needed here ...
      \\i post_pl.sql
    EOL
    gosu postgres pg_ctl stop
    

    我在这里假设shp2pgsql 不需要数据库。确实需要数据库的osm2pgsql 可以放在EOL 之后,就在pg_ctl stop 之前。

    【讨论】:

    • 在我的 Dockerfile 我把这个添加命令: RUN mkdir -p /docker-entrypoint-initdb.d ADD ./osm_stylesheet /docker-entrypoint-initdb.d/osm_stylesheet ADD ./post_pl .sql /docker-entrypoint-initdb.d/post_pl.sql 添加 ./run.sh /docker-entrypoint-initdb.d/run.sh RUN chmod +x /docker-entrypoint-initdb.d/*.sh 但我收到一个错误,即 .sql 文件不存在。
    • 它可能假定 .sql 文件位于不同的目录中。 (或存在权限或用户 ID 问题)尝试指定完整路径名。或者在 /tmp/ 中创建你需要的东西)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多