Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,45 @@ After the file is loaded into the postgis database, you can connect to it locall

You can update the database by re-running the osm2pgsql docker container, it will use
`osm2pgsql-replication` to update the existing database.

## Post-Import Hook

The importer supports an optional post-processing hook that runs after successful import or update cycles. This is useful for creating statistics views, refreshing materialized views, or running custom SQL.

### Environment Variables

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `POST_IMPORT_SQL` | No | _(empty)_ | Path to SQL file executed after successful import/update |
| `POST_IMPORT_SCRIPT` | No | _(empty)_ | Path to shell script (takes precedence over SQL) |
| `POST_IMPORT_FAIL_HARD` | No | `true` | If `true`, post-import failure fails the entire import |

### Execution Behavior

- **Script precedence**: If `POST_IMPORT_SCRIPT` is set and exists, it runs instead of SQL
- **Skip silently**: If neither variable is set or files don't exist, continues without error
- **Connection reuse**: SQL execution inherits database connection from importer environment
- **Logging**: Output prefixed with `[post-import]` to distinguish from import logs

### Example Configuration

```yaml
services:
osm2pgsql:
environment:
# ... other vars ...
POST_IMPORT_SQL: /osm2pgsql-configs/post-import.sql
POST_IMPORT_FAIL_HARD: "true"
volumes:
- ./osm2pgsql-configs:/osm2pgsql-configs:ro
```

### Writing Idempotent SQL

Post-import SQL should be idempotent (safe to run on both create and update cycles):

```sql
-- Drop and recreate pattern (recommended for stats views)
DROP MATERIALIZED VIEW IF EXISTS my_stats;
CREATE MATERIALIZED VIEW my_stats AS SELECT ...;
```
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ services:
PG_ENV_POSTGRES_USER: docker
PG_ENV_POSTGRES_PASSWORD: docker
REGION: north-america/us/delaware
# Output mode: pgsql (default) or flex
OSM2PGSQL_MODE: flex
# Custom Lua config path (optional, only used when OSM2PGSQL_MODE=flex)
OSM2PGSQL_LUA_CONFIG: /osm2pgsql-configs/config.lua
# Performance tuning (auto-detected if not set)
# OSM2PGSQL_CACHE: 8000 # Cache size in MB (use 0 with flat-nodes)
# OSM2PGSQL_FLAT_NODES: /osm/data/flat-nodes.bin # Use flat nodes for PBF > 2GB
# OSM2PGSQL_PROCS: 8 # Parallel threads (auto: min of CPU cores, 8)
restart: on-failure:2
volumes:
# Specify local directory with .pbf file
- ./data:/osm/data
- ./osm2pgsql:/osm2pgsql-configs

postgres:
image: postgis/postgis
Expand Down
4 changes: 3 additions & 1 deletion osm2pgsql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ RUN apt-get clean \
&& rm -rf /var/lib/apt/lists/*

ADD ./osm-importer.sh /usr/local/bin/osm-importer.sh
ADD ./custom.style /user/local/bin/custom.style
ADD ./custom.style /usr/local/bin/custom.style
ADD ./flex-config.lua /usr/local/bin/flex-config.lua
ADD ./compatible.lua /usr/local/bin/compatible.lua
WORKDIR /osm
Loading