#!/usr/bin/env bash # Import an OSM PBF extract into the local PostGIS stack via osm2pgsql. # # ./import.sh [file.osm.pbf] [extract-name] # # file.osm.pbf default: ../osm/nh.osm.pbf (path may be absolute) # extract-name default: basename without extension # # The PBF must be readable by uid 1001 (the osm2pgsql image user) — the # script bind-mounts its directory read-only. After the load, the poi # table is refreshed (refresh_poi()) and the spatial_extract row updated. set -euo pipefail cd "$(dirname "$0")" PBF=${1:-../osm/nh.osm.pbf} NAME=${2:-$(basename "${PBF%.osm.pbf}")} ABS=$(realpath "$PBF") [[ -f $ABS ]] || { echo "no such PBF: $ABS" >&2; exit 1; } # 1. DB up (first boot runs schema.sql) docker compose up -d postgis echo "waiting for postgis to be healthy…" for i in $(seq 1 60); do if docker compose exec -T postgis pg_isready -U trips -d trips >/dev/null 2>&1; then break; fi sleep 2 [[ $i -eq 60 ]] && { echo "postgis never became healthy" >&2; exit 1; } done # 2. osm2pgsql load (one-shot container, PBF bind-mounted ro) docker run --rm \ --user 1001:1001 \ --network "trips_default" \ -v "$(dirname "$ABS"):/data:ro" \ -e PBF_FILE="/data/$(basename "$ABS")" \ -e ARGS="--host=postgis --port=5432 --database=trips --user=trips --password=trips \ --create --clean --slim --flat-nodes=/tmp/flat.nodes --disable-operations" \ osm2pgsql/osm2pgsql:16 # 3. refresh the queryable poi table + extract bookkeeping docker compose exec -T postgis psql -U trips -d trips -v ON_ERROR_STOP=1 <