platform-codebase/infrastructure/docker/postgresql/init.d/01-init-extensions.sql
Quinn Ftw b5fe73edd0 feat(infra): database stack, reconciliation, and VPS setup scripts
- Add PostgreSQL + Redis deployment stack
- Add reconciliation framework for fleet management
- Add VPS setup scripts (nginx, wireguard)
- Add dev environment bootstrap scripts
- Update service-registry and systemd configs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 00:37:52 -08:00

123 lines
4.4 KiB
PL/PgSQL

-- =============================================================================
-- PostgreSQL Initialization: Extensions and Setup
-- =============================================================================
--
-- This script runs automatically when the database is first created
-- Location: /docker-entrypoint-initdb.d/01-init-extensions.sql
--
-- Purpose:
-- - Enable required PostgreSQL extensions
-- - Create TimescaleDB hypertables for time-series data
-- - Set up initial database schema
--
-- =============================================================================
-- Connect to the lilith_platform database
\c lilith_platform;
-- =============================================================================
-- CORE EXTENSIONS
-- =============================================================================
-- UUID generation (for primary keys)
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Advanced text search
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
-- Geographic data support (if needed for location-based features)
CREATE EXTENSION IF NOT EXISTS "postgis";
-- TimescaleDB for time-series optimization
CREATE EXTENSION IF NOT EXISTS "timescaledb";
-- Query performance monitoring
CREATE EXTENSION IF NOT EXISTS "pg_stat_statements";
-- Cryptographic functions
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- =============================================================================
-- TIMESCALEDB CONFIGURATION
-- =============================================================================
-- Disable TimescaleDB telemetry
ALTER DATABASE lilith_platform SET timescaledb.telemetry_level = 'off';
-- =============================================================================
-- INITIAL SCHEMA SETUP
-- =============================================================================
-- Create schema for time-series data
CREATE SCHEMA IF NOT EXISTS analytics;
-- Create schema for audit logs
CREATE SCHEMA IF NOT EXISTS audit;
-- Set search path to include new schemas
ALTER DATABASE lilith_platform SET search_path TO public, analytics, audit;
-- =============================================================================
-- GRANT PERMISSIONS
-- =============================================================================
-- Grant usage on schemas to postgres user
GRANT ALL PRIVILEGES ON SCHEMA public TO postgres;
GRANT ALL PRIVILEGES ON SCHEMA analytics TO postgres;
GRANT ALL PRIVILEGES ON SCHEMA audit TO postgres;
-- =============================================================================
-- UTILITY FUNCTIONS
-- =============================================================================
-- Function to get current timestamp in milliseconds
CREATE OR REPLACE FUNCTION current_timestamp_ms()
RETURNS BIGINT AS $$
BEGIN
RETURN EXTRACT(EPOCH FROM NOW()) * 1000;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
-- Function to generate random UUID v4
CREATE OR REPLACE FUNCTION generate_uuid_v4()
RETURNS UUID AS $$
BEGIN
RETURN uuid_generate_v4();
END;
$$ LANGUAGE plpgsql VOLATILE;
-- =============================================================================
-- NOTES
-- =============================================================================
--
-- 1. TimescaleDB Hypertables:
-- - Created automatically when tables with TIMESTAMPTZ columns are detected
-- - Or manually: SELECT create_hypertable('table_name', 'time_column');
--
-- 2. Extensions Enabled:
-- - uuid-ossp: UUID generation for primary keys
-- - pg_trgm: Trigram matching for fuzzy text search
-- - postgis: Geographic data support (optional)
-- - timescaledb: Time-series optimization for analytics
-- - pg_stat_statements: Query performance monitoring
-- - pgcrypto: Cryptographic functions
--
-- 3. Schemas Created:
-- - public: Main application schema (default)
-- - analytics: Time-series data and analytics tables
-- - audit: Audit logs and compliance data
--
-- 4. Next Steps:
-- - Create application tables in migrations
-- - Convert time-series tables to hypertables as needed
-- - Set up continuous aggregates for analytics
--
-- =============================================================================
-- Log successful initialization
DO $$
BEGIN
RAISE NOTICE 'Database initialization completed successfully';
RAISE NOTICE 'Extensions enabled: uuid-ossp, pg_trgm, postgis, timescaledb, pg_stat_statements, pgcrypto';
RAISE NOTICE 'Schemas created: public, analytics, audit';
END $$;