-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstart_pgsql_pgvector.sh
More file actions
executable file
·176 lines (151 loc) · 6.71 KB
/
start_pgsql_pgvector.sh
File metadata and controls
executable file
·176 lines (151 loc) · 6.71 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# --- Docker configs ---
CONTAINER_NAME="pgsql16_vector"
DATA_VOLUME="pgdata_vector"
# --- POSTGRES config ---
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="password"
DEFAULT_DB="postgres"
TARGET_DB="customer360"
HOST_PORT=5432
# --- SQL schema config ---
SCHEMA_VERSION=251027
SCHEMA_DESCRIPTION="init database schema customer360 for leo bot in CDP and chatbot for end user"
SQL_FILE_PATH="./sql_scripts/customer360_schema.sql"
# --- Parse options ---
RESET_DB=false
for arg in "$@"; do
case $arg in
--reset-db)
RESET_DB=true
shift
;;
esac
done
# --- Function to check PostgreSQL readiness ---
wait_for_postgres() {
local max_attempts=10
local attempt=1
echo "⏳ Checking if PostgreSQL is ready..."
until docker exec -u postgres $CONTAINER_NAME psql -d $DEFAULT_DB -c "SELECT 1;" >/dev/null 2>&1; do
if [ $attempt -ge $max_attempts ]; then
echo "❌ Error: PostgreSQL is not ready after $max_attempts attempts."
exit 1
fi
echo "⏳ Attempt $attempt/$max_attempts: Waiting for PostgreSQL..."
sleep 2
((attempt++))
done
echo "🟢 PostgreSQL is ready."
}
# --- Check if container exists ---
if docker ps -a --format '{{.Names}}' | grep -Eq "^${CONTAINER_NAME}$"; then
# If exists but not running, start it
if ! docker ps --format '{{.Names}}' | grep -Eq "^${CONTAINER_NAME}$"; then
echo "🔄 Starting existing container '${CONTAINER_NAME}'..."
docker start "$CONTAINER_NAME"
wait_for_postgres
else
echo "🟢 PostgreSQL container '${CONTAINER_NAME}' is already running."
wait_for_postgres
fi
else
# Create volume if needed
if ! docker volume ls | grep -q "$DATA_VOLUME"; then
docker volume create "$DATA_VOLUME"
fi
echo "🚀 Launching new PostgreSQL container '${CONTAINER_NAME}'..."
docker run -d \
--name $CONTAINER_NAME \
-e POSTGRES_USER=$POSTGRES_USER \
-e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
-e POSTGRES_DB=$DEFAULT_DB \
-p $HOST_PORT:5432 \
-v $DATA_VOLUME:/var/lib/postgresql/data \
postgis/postgis:16-3.5
wait_for_postgres
# Install pgvector inside the container
echo "📦 Installing pgvector extension..."
docker exec -u root $CONTAINER_NAME bash -c "apt-get update && apt-get install -y postgresql-16-pgvector"
fi
# --- Fix collation version mismatch ---
echo "🔧 Checking and fixing collation version mismatch for 'postgres' and 'template1'..."
docker exec -u postgres $CONTAINER_NAME psql -d $DEFAULT_DB -c "ALTER DATABASE postgres REFRESH COLLATION VERSION;" || echo "⚠️ Warning: Failed to refresh 'postgres'."
docker exec -u postgres $CONTAINER_NAME psql -d template1 -c "ALTER DATABASE template1 REFRESH COLLATION VERSION;" || echo "⚠️ Warning: Failed to refresh 'template1'."
# --- Drop DB if requested ---
if [ "$RESET_DB" = true ]; then
echo "⚠️ --reset-db detected. Dropping database '${TARGET_DB}' if exists..."
docker exec -u postgres $CONTAINER_NAME psql -d $DEFAULT_DB -c "DROP DATABASE IF EXISTS ${TARGET_DB};"
fi
# --- Create DB if not exists ---
echo "🔄 Checking if database '${TARGET_DB}' exists..."
DB_EXISTS=$(docker exec -u postgres $CONTAINER_NAME psql -d $DEFAULT_DB -tc "SELECT 1 FROM pg_database WHERE datname='${TARGET_DB}';" | tr -d '[:space:]')
if [ "$DB_EXISTS" != "1" ]; then
echo "🚀 Creating database '${TARGET_DB}'..."
docker exec -u postgres $CONTAINER_NAME psql -d $DEFAULT_DB -c "CREATE DATABASE ${TARGET_DB};"
fi
# --- Ensure connection to target database ---
wait_for_postgres_target() {
local max_attempts=5
local attempt=1
echo "⏳ Checking if database '${TARGET_DB}' is accessible..."
until docker exec -u postgres $CONTAINER_NAME psql -d $TARGET_DB -c "SELECT 1;" >/dev/null 2>&1; do
if [ $attempt -ge $max_attempts ]; then
echo "❌ Error: Database '${TARGET_DB}' is not accessible after $max_attempts attempts."
exit 1
fi
echo "⏳ Attempt $attempt/$max_attempts: Waiting for database '${TARGET_DB}'..."
sleep 2
((attempt++))
done
echo "🟢 Database '${TARGET_DB}' is accessible."
}
wait_for_postgres_target
# --- Enable extensions ---
echo "🔧 Enabling extensions in '${TARGET_DB}'..."
docker exec -u postgres $CONTAINER_NAME psql -d $TARGET_DB -c "CREATE EXTENSION IF NOT EXISTS vector;" || { echo "❌ Failed to enable 'vector'"; exit 1; }
docker exec -u postgres $CONTAINER_NAME psql -d $TARGET_DB -c "CREATE EXTENSION IF NOT EXISTS postgis;" || { echo "❌ Failed to enable 'postgis'"; exit 1; }
# --- Create schema_migrations table ---
echo "🔧 Creating schema_migrations table..."
docker exec -u postgres $CONTAINER_NAME psql -d $TARGET_DB -c "
CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
applied_at TIMESTAMP DEFAULT NOW(),
description TEXT
);
"
# --- Check current schema version ---
echo "🔍 Checking current schema version..."
CURRENT_VERSION=$(docker exec -u postgres $CONTAINER_NAME psql -d $TARGET_DB -t -c "SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1;" 2>/dev/null | tr -d '[:space:]' || echo "0")
if [ -z "$CURRENT_VERSION" ]; then CURRENT_VERSION=0; fi
echo "ℹ️ Current schema version: $CURRENT_VERSION"
# --- Function to apply migration ---
apply_migration() {
local version=$1
local description=$2
local sql_file_path=$3
echo "🚀 Applying migration for version $version: $description"
if [[ ! -f "$sql_file_path" ]]; then
echo "❌ SQL file not found: $sql_file_path"
exit 1
fi
docker exec -i -u postgres "$CONTAINER_NAME" psql -d "$TARGET_DB" < "$sql_file_path" || { echo "❌ Failed to apply migration $version"; exit 1; }
docker exec -u postgres "$CONTAINER_NAME" psql -d "$TARGET_DB" -c \
"INSERT INTO schema_migrations (version, description, applied_at) VALUES ($version, '$description', NOW());" || { echo "❌ Failed to record migration $version"; exit 1; }
echo "✅ Migration $version applied successfully."
}
# --- Apply initial schema migration if needed ---
if [ $CURRENT_VERSION -lt $SCHEMA_VERSION ]; then
apply_migration $SCHEMA_VERSION "$SCHEMA_DESCRIPTION" "$SQL_FILE_PATH"
fi
# --- Verify all tables exist ---
TABLES=("chat_messages" "chat_message_embeddings" "places" "schema_migrations" "system_users" "conversational_context" "knowledge_sources" "knowledge_chunks" "customer_profile" "transactional_context" "customer_metrics" "tenant_metrics_config")
for table in "${TABLES[@]}"; do
docker exec -u postgres $CONTAINER_NAME psql -d $TARGET_DB -tc "SELECT 1 FROM pg_tables WHERE tablename = '$table'" | grep -q 1 || { echo "❌ Table '$table' missing"; exit 1; }
done
echo "✅ PostgreSQL 16 + PostGIS + pgvector is ready."
echo " ➜ DB: $TARGET_DB"
echo " ➜ Tables: ${TABLES[*]}"
echo " ➜ Extensions: vector, postgis"
echo " ➜ Schema Version: $SCHEMA_VERSION"
echo " ➜ Port: $HOST_PORT"