#!/usr/bin/env bash set -euo pipefail DBOPS=${DBOPS_ROOT:-/data/adaptai/platform/dbops} PGROOT=$DBOPS/binaries/postgres PGBIN=$PGROOT/bin PGDATA=$DBOPS/data/postgres PGLOG=$DBOPS/logs/postgres PORT=${PG_PORT:-17532} mkdir -p "$PGROOT" "$PGDATA" "$PGLOG" if [ ! -x "$PGBIN/postgres" ]; then echo "[postgres] building from source under $PGROOT" tmp=$(mktemp -d) cd "$tmp" # Try latest stable releases first for ver in 17.4 17.3 17.2 17.1 17.0 16.4 16.3 16.2 16.1 16.0; do url="https://ftp.postgresql.org/pub/source/v$ver/postgresql-$ver.tar.gz" if curl -fsSL "$url" -o pgsrc.tgz; then tar -xzf pgsrc.tgz && cd postgresql-* && \ ./configure --prefix="$PGROOT" --without-readline --without-zlib >/dev/null && \ make -j"$(nproc)" world-bin >/dev/null && \ make install-world-bin >/dev/null && break || true fi done cd / rm -rf "$tmp" fi if [ ! -f "$PGDATA/PG_VERSION" ]; then echo "[postgres] initdb" "$PGBIN/initdb" -D "$PGDATA" -U postgres -A trust >/dev/null # Basic config cat >> "$PGDATA/postgresql.conf" <> "$PGDATA/pg_hba.conf" echo "host all all ::1/128 trust" >> "$PGDATA/pg_hba.conf" fi # Install pgvector if missing if [ ! -f "$PGROOT/share/extension/vector.control" ]; then echo "[postgres] building pgvector" tmp=$(mktemp -d) cd "$tmp" # Try latest tags; fallback to main for tag in v0.7.4 v0.7.3 v0.7.2; do if curl -fsSL "https://github.com/pgvector/pgvector/archive/refs/tags/$tag.tar.gz" -o pgvector.tgz; then tar -xzf pgvector.tgz && cd pgvector-* && \ make USE_PGXS=1 PG_CONFIG="$PGBIN/pg_config" >/dev/null && \ make USE_PGXS=1 PG_CONFIG="$PGBIN/pg_config" install >/dev/null && break || true fi done cd / rm -rf "$tmp" fi # Create extension in template1 asynchronously after server starts ( sleep 4; "$PGBIN/psql" -U postgres -h 127.0.0.1 -p "$PORT" -d postgres -c "CREATE EXTENSION IF NOT EXISTS vector;" >/dev/null 2>&1 || true ) & exec "$PGBIN/postgres" -D "$PGDATA" -p "$PORT"