File size: 2,191 Bytes
30b9f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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" <<CONF
listen_addresses = '*'
port = $PORT
logging_collector = on
log_directory = '$PGLOG'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
CONF
  echo "host all all 127.0.0.1/32 trust" >> "$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"