diff --git a/databases/scripts/create-pipeline-admin.sql b/databases/scripts/create-pipeline-admin.sql new file mode 100644 index 0000000..15739e8 --- /dev/null +++ b/databases/scripts/create-pipeline-admin.sql @@ -0,0 +1,37 @@ +-- Create pipeline_admin user for existing database +-- This script can be run manually on existing PostgreSQL instances + +-- Create pipeline_admin user if it doesn't exist +DO $$ +BEGIN + IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'pipeline_admin') THEN + CREATE USER pipeline_admin WITH PASSWORD 'secure_pipeline_2024'; + RAISE NOTICE 'Created user pipeline_admin'; + ELSE + -- Update password in case it's different + ALTER USER pipeline_admin WITH PASSWORD 'secure_pipeline_2024'; + RAISE NOTICE 'Updated password for existing user pipeline_admin'; + END IF; +END +$$; + +-- Ensure pipeline_admin has superuser privileges (needed for migrations) +ALTER USER pipeline_admin WITH SUPERUSER; + +-- Grant all privileges on existing databases +GRANT ALL PRIVILEGES ON DATABASE postgres TO pipeline_admin; +GRANT ALL PRIVILEGES ON DATABASE dev_pipeline TO pipeline_admin; + +-- Connect to dev_pipeline and grant schema permissions +\c dev_pipeline; +GRANT ALL ON SCHEMA public TO pipeline_admin; +GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO pipeline_admin; +GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO pipeline_admin; +GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO pipeline_admin; + +-- Set default privileges for future objects +ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO pipeline_admin; +ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO pipeline_admin; +ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO pipeline_admin; + +\echo 'Pipeline admin user setup completed successfully' diff --git a/databases/scripts/init.sql b/databases/scripts/init.sql index 856ce32..baac1ef 100644 --- a/databases/scripts/init.sql +++ b/databases/scripts/init.sql @@ -6,6 +6,14 @@ CREATE DATABASE dev_pipeline; -- Create users CREATE USER n8n_user WITH PASSWORD 'n8n_secure_2024'; CREATE USER gitea_user WITH PASSWORD 'gitea_secure_2024'; +-- Create pipeline_admin user if it doesn't exist +DO $$ +BEGIN + IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'pipeline_admin') THEN + CREATE USER pipeline_admin WITH PASSWORD 'secure_pipeline_2024'; + END IF; +END +$$; -- Grant permissions GRANT ALL PRIVILEGES ON DATABASE n8n_db TO n8n_user; diff --git a/docker-compose.yml b/docker-compose.yml index a3c262d..5cbbe59 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,7 @@ services: POSTGRES_DB: dev_pipeline volumes: - postgres_data:/var/lib/postgresql/data + - ./databases/scripts/init.sql:/docker-entrypoint-initdb.d/init.sql ports: - "5432:5432" networks: diff --git a/scripts/fix-postgres-user.sh b/scripts/fix-postgres-user.sh new file mode 100644 index 0000000..1b01764 --- /dev/null +++ b/scripts/fix-postgres-user.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Fix PostgreSQL user creation for existing deployments +# This script creates the pipeline_admin user in an existing PostgreSQL container + +echo "🔧 Fixing PostgreSQL user authentication..." + +# Wait for PostgreSQL to be ready +echo "⏳ Waiting for PostgreSQL to be ready..." +until docker exec pipeline_postgres pg_isready -U postgres > /dev/null 2>&1; do + echo " PostgreSQL is not ready yet, waiting..." + sleep 2 +done + +echo "✅ PostgreSQL is ready" + +# Execute the user creation script +echo "👤 Creating pipeline_admin user..." +docker exec -i pipeline_postgres psql -U postgres -d postgres < ./databases/scripts/create-pipeline-admin.sql + +if [ $? -eq 0 ]; then + echo "✅ Pipeline admin user created successfully" + echo "🚀 You can now restart the migrations service:" + echo " docker compose restart migrations" +else + echo "❌ Failed to create pipeline admin user" + exit 1 +fi