diff --git a/bin/import-sql b/bin/import-sql index 215727fc..c2af86dc 100755 --- a/bin/import-sql +++ b/bin/import-sql @@ -4,6 +4,16 @@ set -o pipefail set -o nounset shopt -s nullglob +# For backward compatibility, allow both PG* and POSTGRES_* forms, +# with the non-standard POSTGRES_* form taking precedence. +# An error will be raised if neither form is given, except for the PGPORT +export PGHOST="${POSTGRES_HOST:-${PGHOST?}}" +export PGDATABASE="${POSTGRES_DB:-${PGDATABASE?}}" +export PGUSER="${POSTGRES_USER:-${PGUSER?}}" +export PGPASSWORD="${POSTGRES_PASSWORD:-${PGPASSWORD?}}" +export PGPORT="${POSTGRES_PORT:-${PGPORT:-5432}}" + + function exec_psql_file() { local file_name="$1" # Allws additional parameters to be passed to psql @@ -14,12 +24,8 @@ function exec_psql_file() { echo "Importing $file_name (md5 $(md5sum < "$file_name") $(wc -l < "$file_name") lines) into Postgres..." # shellcheck disable=SC2154 - PGPASSWORD="$POSTGRES_PASSWORD" psql \ + psql \ -v ON_ERROR_STOP="1" \ - --host="$POSTGRES_HOST" \ - ${POSTGRES_PORT:+ --port="$POSTGRES_PORT"} \ - --dbname="$POSTGRES_DB" \ - --username="$POSTGRES_USER" \ -c '\timing on' \ -f "$file_name" \ "${psql_opts[@]}" diff --git a/docker/generate-vectortiles/README.md b/docker/generate-vectortiles/README.md index 37a7d2b3..29ad4c8f 100644 --- a/docker/generate-vectortiles/README.md +++ b/docker/generate-vectortiles/README.md @@ -12,10 +12,15 @@ You need to provide the database credentials and run `generate-vectortiles`. docker run --rm \ -v $(pwd)/project.tm2source:/tm2source \ -v $(pwd):/export \ - -e POSTGRES_USER="osm" \ - -e POSTGRES_PASSWORD="osm" \ - -e POSTGRES_HOST="127.0.0.1" \ - -e POSTGRES_DB="osm" \ - -e POSTGRES_PORT="5432" \ + -e PGHOST="127.0.0.1" \ + -e PGDATABASE="openmaptiles" \ + -e PGUSER="openmaptiles" \ + -e PGPASSWORD="openmaptiles" \ openmaptiles/generate-vectortiles ``` + +Optional environment variables: +* `PGPORT` (defaults to `5432`) + +Legacy env variables are still supported, but they are not recommended: +`POSTGRES_HOST`,`POSTGRES_DB`, `POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_PORT` diff --git a/docker/generate-vectortiles/utils.sh b/docker/generate-vectortiles/utils.sh index 791a4aba..0aa306ef 100755 --- a/docker/generate-vectortiles/utils.sh +++ b/docker/generate-vectortiles/utils.sh @@ -5,6 +5,15 @@ set -o pipefail readonly DEST_PROJECT_DIR="/tmp/project" readonly DEST_PROJECT_FILE="${DEST_PROJECT_DIR%%/}/data.yml" +# For backward compatibility, allow both PG* and POSTGRES_* forms, +# with the non-standard POSTGRES_* form taking precedence. +# An error will be raised if neither form is given, except for the PGPORT +PGHOST="${POSTGRES_HOST:-${PGHOST?}}" +PGDATABASE="${POSTGRES_DB:-${PGDATABASE?}}" +PGUSER="${POSTGRES_USER:-${PGUSER?}}" +PGPASSWORD="${POSTGRES_PASSWORD:-${PGPASSWORD?}}" +PGPORT="${POSTGRES_PORT:-${PGPORT:-5432}}" + # project config will be copied to new folder because we # modify the source configuration function copy_source_project() { @@ -19,11 +28,11 @@ function cleanup_dest_project() { # replace database connection with postgis container connection function replace_db_connection() { - local replace_expr_1="s|host: .*|host: \"$POSTGRES_HOST\"|g" - local replace_expr_2="s|port: .*|port: \"$POSTGRES_PORT\"|g" - local replace_expr_3="s|dbname: .*|dbname: \"$POSTGRES_DB\"|g" - local replace_expr_4="s|user: .*|user: \"$POSTGRES_USER\"|g" - local replace_expr_5="s|password: .*|password: \"$POSTGRES_PASSWORD\"|g" + local replace_expr_1="s|host: .*|host: \"$PGHOST\"|g" + local replace_expr_2="s|port: .*|port: \"$PGPORT\"|g" + local replace_expr_3="s|dbname: .*|dbname: \"$PGDATABASE\"|g" + local replace_expr_4="s|user: .*|user: \"$PGUSER\"|g" + local replace_expr_5="s|password: .*|password: \"$PGPASSWORD\"|g" sed -i "$replace_expr_1" "$DEST_PROJECT_FILE" sed -i "$replace_expr_2" "$DEST_PROJECT_FILE" diff --git a/docker/import-sql/README.md b/docker/import-sql/README.md index d6fe1de1..27e1ef09 100644 --- a/docker/import-sql/README.md +++ b/docker/import-sql/README.md @@ -1,6 +1,6 @@ # !! OBSOLETE !! -This image is obsolete, and should not be used. Use `openmaptiles-tools` image instead. `import_sql.sh` was copied to `/bin`. +This image is obsolete, and should not be used. Use `openmaptiles-tools` image instead. `import_sql.sh` was copied to `/bin` and should be used as `import-sql` command. ## Import SQL files into PostgreSQL [![Docker Automated buil](https://img.shields.io/docker/automated/openmaptiles/import-sql.svg)](https://hub.docker.com/r/openmaptiles/import-sql/)