update seeder
This commit is contained in:
Executable
+75
@@ -0,0 +1,75 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Script to fix PostgreSQL permissions using sudo (no password prompt for postgres user)
|
||||
# This script reads DB_USER and DB_NAME from .env and grants necessary permissions
|
||||
|
||||
# Function to load .env file properly (handles comments and special characters)
|
||||
load_env() {
|
||||
if [ -f .env ]; then
|
||||
while IFS= read -r line || [ -n "$line" ]; do
|
||||
[[ "$line" =~ ^[[:space:]]*# ]] && continue
|
||||
[[ -z "${line// }" ]] && continue
|
||||
|
||||
if [[ "$line" =~ ^[[:space:]]*([^#=]+)=(.*)$ ]]; then
|
||||
key="${BASH_REMATCH[1]%% *}"
|
||||
value="${BASH_REMATCH[2]}"
|
||||
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
|
||||
|
||||
echo "Fixing PostgreSQL permissions for user: $DB_USER on database: $DB_NAME"
|
||||
echo "Using sudo to connect as postgres user (no password needed)"
|
||||
echo ""
|
||||
|
||||
# Connect as postgres superuser using sudo (no password prompt)
|
||||
sudo -u postgres psql -d postgres <<EOF
|
||||
-- Make the user the owner of the database (PostgreSQL 15+ requirement)
|
||||
ALTER DATABASE $DB_NAME OWNER TO $DB_USER;
|
||||
|
||||
-- Connect to the target database to grant schema permissions
|
||||
\c $DB_NAME
|
||||
|
||||
-- Make the user the owner of the public schema (PostgreSQL 15+ requirement)
|
||||
ALTER SCHEMA public OWNER TO $DB_USER;
|
||||
|
||||
-- Grant all necessary permissions (redundant but safe)
|
||||
GRANT CONNECT ON DATABASE $DB_NAME TO $DB_USER;
|
||||
GRANT USAGE ON SCHEMA public TO $DB_USER;
|
||||
GRANT CREATE ON SCHEMA public TO $DB_USER;
|
||||
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $DB_USER;
|
||||
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $DB_USER;
|
||||
GRANT ALL PRIVILEGES ON ALL FUNCTIONS 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;
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO $DB_USER;
|
||||
|
||||
-- For PostgreSQL 15+ - ensure full database access
|
||||
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."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user