Files
n8n-install/scripts/init_databases.sh
Yury Kossakovsky f32c92287e feat: add isolated postgresql databases for services
add init_databases.sh script that creates dedicated databases for
langfuse, lightrag, nocodb, postiz, and waha during install/update.
update connection strings to use service-specific databases instead
of sharing the default postgres database.
2025-12-25 11:25:31 -07:00

65 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# init_databases.sh - Create isolated PostgreSQL databases for services
# =============================================================================
# This script runs during install/update and creates databases if they don't exist.
# Safe to run multiple times - only creates missing databases.
#
# Usage: Called automatically from install.sh and update.sh
# =============================================================================
source "$(dirname "$0")/utils.sh" && init_paths
# List of databases to create (add new services here)
# Note: n8n uses the default 'postgres' database
DATABASES=(
"langfuse"
"lightrag"
"nocodb"
"postiz"
"waha"
)
log_header "Initializing PostgreSQL Databases"
# Ensure postgres is running and healthy
log_info "Waiting for PostgreSQL to be ready..."
MAX_WAIT=60
WAITED=0
while ! docker exec postgres pg_isready -U postgres > /dev/null 2>&1; do
if [ $WAITED -ge $MAX_WAIT ]; then
log_error "PostgreSQL did not become ready in ${MAX_WAIT}s"
exit 1
fi
sleep 1
((WAITED++))
done
log_success "PostgreSQL is ready"
# Create databases
CREATED=0
EXISTING=0
for db in "${DATABASES[@]}"; do
# Check if database exists
EXISTS=$(docker exec postgres psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$db'" 2>/dev/null)
if [ "$EXISTS" = "1" ]; then
log_info "Database '$db' already exists"
((EXISTING++))
else
log_info "Creating database '$db'..."
if docker exec postgres psql -U postgres -c "CREATE DATABASE $db" > /dev/null 2>&1; then
log_success "Database '$db' created"
((CREATED++))
else
log_error "Failed to create database '$db'"
fi
fi
done
log_divider
log_success "Database initialization complete: $CREATED created, $EXISTING already existed"