#!/usr/bin/env bash # Starts a throwaway PostgreSQL with the pgvector extension, with no Docker, and prepares the # ragdb database. Use it when docker compose is not available. It is what produced the # transcripts in docs/output/ in this repository's authoring environment. # # Requires PostgreSQL 14+ and the pgvector package (Debian/Ubuntu: apt install postgresql-16 postgresql-16-pgvector). # Environment: PGDATA (default /tmp/pgrag/data), PGPORT (default 5439). # Afterwards: export RAG_PG_URL=jdbc:postgresql://127.0.0.1:${PGPORT:-5439}/ragdb set -euo pipefail PGDATA="${PGDATA:-/tmp/pgrag/data}" PGPORT="${PGPORT:-5439}" SOCKDIR="$(dirname "$PGDATA")" BIN="$(ls -d /usr/lib/postgresql/*/bin 2>/dev/null | sort -V | tail -1)" [ -x "$BIN/initdb" ] || { echo "PostgreSQL server binaries not found under /usr/lib/postgresql" >&2; exit 1; } HERE="$(cd "$(dirname "$0")/.." && pwd)" # PostgreSQL refuses to run as root, so hand the work to the postgres user in that case. as_pg() { if [ "$(id -u)" = 0 ]; then su postgres -c "$*"; else bash -c "$*"; fi; } mkdir -p "$SOCKDIR" [ "$(id -u)" = 0 ] && chown postgres "$SOCKDIR" if [ ! -d "$PGDATA/base" ]; then as_pg "'$BIN/initdb' -D '$PGDATA' -A trust >'$SOCKDIR/initdb.log' 2>&1" fi if ! as_pg "'$BIN/pg_ctl' -D '$PGDATA' status" >/dev/null 2>&1; then as_pg "'$BIN/pg_ctl' -D '$PGDATA' -o \"-p $PGPORT -c listen_addresses=127.0.0.1 -c unix_socket_directories=$SOCKDIR\" -l '$SOCKDIR/pg.log' -w start" fi PSQL="$BIN/psql -h $SOCKDIR -p $PGPORT -U postgres -v ON_ERROR_STOP=1 -q" as_pg "$PSQL -d postgres -tc \"select 1 from pg_roles where rolname='raguser'\"" | grep -q 1 \ || as_pg "$PSQL -d postgres -c \"create role raguser login superuser password 'ragpass'\"" as_pg "$PSQL -d postgres -tc \"select 1 from pg_database where datname='ragdb'\"" | grep -q 1 \ || as_pg "$PSQL -d postgres -c 'create database ragdb owner raguser'" as_pg "$PSQL -d ragdb -c 'create extension if not exists vector'" as_pg "$PSQL -d ragdb -f '$HERE/init.sql'" echo "PostgreSQL is up on 127.0.0.1:$PGPORT (database ragdb, user raguser, password ragpass)" echo "export RAG_PG_URL=jdbc:postgresql://127.0.0.1:$PGPORT/ragdb"