1

I am working on this Bash Script for Dockerfile:

#!/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

i wan to add two import commands after 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`

My question is, can it work? Can we import data when we inside psql? And if yes, how can i do it?

TNX

Andrey Ramnikov

Andrew Ramnikov
  • 783
  • 2
  • 9
  • 30

1 Answers1

0

You can just include it into the here-document, using \i (you probably need to double the backslash) Assuming your post_pl.sql is in the current directory (otherwise specify the full pathname)

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

I am assuming here that shp2pgsql does not need the database. osm2pgsql which does need the database could be placed right after the EOL, just before the pg_ctl stop.

joop
  • 4,330
  • 1
  • 15
  • 26
  • In my Dockerfile i put this ADD commands: 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 ADD ./run.sh /docker-entrypoint-initdb.d/run.sh RUN chmod +x /docker-entrypoint-initdb.d/*.sh** but i get an error that .sql file is not there. – Andrew Ramnikov Feb 24 '16 at 15:19
  • It probably assumes the .sql file to be in a different directory. (or there is a permission- or userid-problem) Try specifying the full pathname. Or create evething you need in /tmp/ ) – joop Feb 25 '16 at 09:32