#!/bin/bash # Script to fix PostgreSQL permissions for schema creation # This script reads DB_USER, DB_NAME, DB_HOST, and DB_PORT from .env and grants necessary permissions # Function to load .env file properly (handles comments and special characters) load_env() { if [ -f .env ]; then # Read .env file line by line, skipping comments and empty lines while IFS= read -r line || [ -n "$line" ]; do # Skip comments and empty lines [[ "$line" =~ ^[[:space:]]*# ]] && continue [[ -z "${line// }" ]] && continue # Export variable if it contains = if [[ "$line" =~ ^[[:space:]]*([^#=]+)=(.*)$ ]]; then key="${BASH_REMATCH[1]%% *}" value="${BASH_REMATCH[2]}" # Remove leading/trailing whitespace and quotes key=$(echo "$key" | xargs) value=$(echo "$value" | sed -e 's/^["'\'']//' -e 's/["'\'']$//' | xargs) export "$key=$value" fi done < .env else echo "Error: .env file not found" exit 1 fi } # Load environment variables load_env # Check if required variables are set if [ -z "$DB_USER" ] || [ -z "$DB_NAME" ]; then echo "Error: DB_USER or DB_NAME not set in .env file" exit 1 fi # Set defaults for host and port if not set DB_HOST=${DB_HOST:-localhost} DB_PORT=${DB_PORT:-5432} echo "Fixing PostgreSQL permissions for user: $DB_USER on database: $DB_NAME" echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT" echo "You will be prompted for the PostgreSQL superuser password (usually 'postgres')" echo "" # Connect as postgres superuser and grant permissions # Use -h for host and -p for port to force TCP/IP connection (password auth) psql -h "$DB_HOST" -p "$DB_PORT" -U postgres -d postgres <