# Cluster build of the OSINT Postgres image (amd64).
#
# Identical to `Dockerfile.pg` in sirius/osint-dashboard EXCEPT the base image
# is pinned to Debian bookworm AND to the 16.16 minor. The upstream file's bare
# `FROM postgres:16` now resolves to Debian trixie, where
# postgresql-16-postgis-3 (3.7.0+pgdg13) fails to load against PostgreSQL 16.15:
#   could not load library "/usr/lib/postgresql/16/lib/postgis-3.so":
#   undefined symbol: heap_datum_check
# On bookworm (+pgdg12) both extensions load and create_hypertable() works.
#
# PostgreSQL minor and the TimescaleDB package build MUST match. TimescaleDB
# publishes one .so per PG minor (the packagecloud suffix `-1616` means built
# against 16.16). PostgreSQL 16.16/17.12/18.7 added the PortalDrop() "while it
# is being set up" check, which the <=2.32.0 background scheduler trips on, so
# every policy job fails with `cannot drop portal ""` (timescale#11038, fixed
# in 2.32.1). Both halves are pinned here so a rebuild cannot silently drift
# back onto a mismatched pair.
#
# Resulting versions: PostgreSQL 16.16, postgis 3.7.0, timescaledb 2.32.1.
# After a pod recreate run `ALTER EXTENSION timescaledb UPDATE TO '2.32.1'`.
FROM postgres:16.16-bookworm

RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates curl gnupg lsb-release && \
    curl -fsSL https://packagecloud.io/install/repositories/timescale/timescaledb/script.deb.sh | bash && \
    apt-get update && apt-get install -y --no-install-recommends \
        postgresql-16-postgis-3 \
        timescaledb-2-postgresql-16=2.32.1~debian12-1616 \
        timescaledb-2-loader-postgresql-16=2.32.1~debian12-1616 && \
    mkdir -p /etc/postgresql/16/main/conf.d && \
    echo "shared_preload_libraries = 'timescaledb'" > /etc/postgresql/16/main/conf.d/timescaledb.conf && \
    rm -rf /var/lib/apt/lists/*

# The official postgres entrypoint remains the CMD and handles
# POSTGRES_USER / POSTGRES_DB / POSTGRES_PASSWORD and pg_hba setup.
