Files
calendars/docker/files/docker-entrypoint-initdb.d/init-databases.sh
Sylvain Zimmer a36348ead1 🎉(all) bootstrap the Calendars project
This repository was forked from Drive in late December 2025 and
boostraped as a minimal demo of backend+caldav server+frontend
integration. There is much left to do and to fix!
2026-01-09 00:51:25 +01:00

47 lines
1.4 KiB
Bash

#!/bin/bash
# Initialize multiple databases on the same PostgreSQL server
# This script runs automatically when the PostgreSQL container starts for the first time
set -e
set -u
# Function to create a database and user if they don't exist
create_database() {
local database=$1
local user=$2
local password=$3
echo "Creating database '$database' with user '$user'..."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
-- Create user if not exists
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '$user') THEN
CREATE USER $user WITH PASSWORD '$password';
END IF;
END
\$\$;
-- Create database if not exists
SELECT 'CREATE DATABASE $database OWNER $user'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$database')\gexec
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE $database TO $user;
EOSQL
echo "Database '$database' created successfully."
}
# Create databases for all services
# The main 'calendar' database is created by default via POSTGRES_DB
# DAViCal database
create_database "davical" "davical" "davical_pass"
# Keycloak database
create_database "keycloak" "keycloak" "keycloak_pass"
echo "All databases initialized successfully!"