85 lines
2.9 KiB
Bash
85 lines
2.9 KiB
Bash
#!/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 <<EOF
|
|
-- Grant usage on the database
|
|
GRANT CONNECT ON DATABASE $DB_NAME TO $DB_USER;
|
|
|
|
-- Grant usage on the public schema
|
|
GRANT USAGE ON SCHEMA public TO $DB_USER;
|
|
|
|
-- Grant create privileges on the public schema
|
|
GRANT CREATE ON SCHEMA public TO $DB_USER;
|
|
|
|
-- Grant all privileges on all tables in public schema (current and future)
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $DB_USER;
|
|
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $DB_USER;
|
|
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $DB_USER;
|
|
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO $DB_USER;
|
|
|
|
-- For PostgreSQL 15+
|
|
GRANT ALL ON DATABASE $DB_NAME TO $DB_USER;
|
|
EOF
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo ""
|
|
echo "✓ Permissions granted successfully!"
|
|
echo "You can now run: npm run db:create"
|
|
else
|
|
echo ""
|
|
echo "✗ Failed to grant permissions. Please check the error above."
|
|
echo ""
|
|
echo "Alternative: You can run the SQL commands manually:"
|
|
echo " psql -h $DB_HOST -p $DB_PORT -U postgres -d postgres"
|
|
echo " Then execute the GRANT commands from fix-db-permissions.sql"
|
|
exit 1
|
|
fi
|
|
|