add database initialization scripts

This commit is contained in:
2026-03-08 12:17:10 +08:00
parent 2d90a4caac
commit e524be15e5
3 changed files with 112 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
#!/bin/bash
# Initialize database user and database
# This script runs before SQL files in docker-entrypoint-initdb.d
# It must be named with 00_ prefix to run first
set -e
APP_DB_NAME="${APP_DB_NAME:-ai_conversations}"
APP_DB_USER="${APP_DB_USER:-myapp_user}"
APP_DB_PASSWORD="${APP_DB_PASSWORD:-secure_password_123}"
echo "Creating database user: $APP_DB_USER"
# Create user
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_user WHERE usename = '$APP_DB_USER') THEN
CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASSWORD';
END IF;
END
\$\$;
ALTER USER $APP_DB_USER CREATEDB;
EOSQL
echo "Creating database: $APP_DB_NAME"
# Create database
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
SELECT 'CREATE DATABASE $APP_DB_NAME'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$APP_DB_NAME')\gexec
GRANT ALL PRIVILEGES ON DATABASE $APP_DB_NAME TO $APP_DB_USER;
EOSQL
echo "Granting schema privileges"
# Grant schema privileges
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$APP_DB_NAME" <<-EOSQL
GRANT ALL ON SCHEMA public TO $APP_DB_USER;
EOSQL
echo "Database initialization complete!"

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# Run SQL initialization files in the correct database context
# This script runs after 00_init_user.sh creates the database
set -e
APP_DB_NAME="${APP_DB_NAME:-ai_conversations}"
echo "Running SQL initialization files in database: $APP_DB_NAME"
# Run create_conv_store.sql
if [ -f /docker-entrypoint-initdb.d/create_conv_store.sql ]; then
echo "Executing create_conv_store.sql..."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$APP_DB_NAME" -f /docker-entrypoint-initdb.d/create_conv_store.sql
fi
# Run create_prompt_config.sql
if [ -f /docker-entrypoint-initdb.d/create_prompt_config.sql ]; then
echo "Executing create_prompt_config.sql..."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$APP_DB_NAME" -f /docker-entrypoint-initdb.d/create_prompt_config.sql
fi
echo "SQL initialization files completed!"

View File

@@ -0,0 +1,48 @@
#!/bin/bash
# Database initialization script
# This script runs all SQL initialization files in the correct order
set -e
DB_NAME="${POSTGRES_DB:-ai_conversations}"
DB_USER="${POSTGRES_USER:-myapp_user}"
DB_PASSWORD="${POSTGRES_PASSWORD:-secure_password_123}"
DB_HOST="${POSTGRES_HOST:-localhost}"
DB_PORT="${POSTGRES_PORT:-5432}"
export PGPASSWORD="$DB_PASSWORD"
echo "Initializing database: $DB_NAME on $DB_HOST:$DB_PORT"
# Wait for PostgreSQL to be ready
until psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c '\q' 2>/dev/null; do
echo "Waiting for PostgreSQL to be ready..."
sleep 2
done
echo "PostgreSQL is ready!"
# Create database if it doesn't exist
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres <<EOF
SELECT 'CREATE DATABASE $DB_NAME'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_NAME')\gexec
EOF
# Grant privileges
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres <<EOF
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF
# Run initialization scripts in order
echo "Running database initialization scripts..."
# 1. Create conversation store tables
echo "Creating conversation store tables..."
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f /docker-entrypoint-initdb.d/create_conv_store.sql
# 2. Create prompt configuration tables
echo "Creating prompt configuration tables..."
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f /docker-entrypoint-initdb.d/create_prompt_config.sql
echo "Database initialization complete!"