105 lines
4.6 KiB
Bash
105 lines
4.6 KiB
Bash
![]() |
#!/bin/bash
|
||
|
# Script to install PostgreSQL, create user with appropriate permissions, and set up the database if not ready
|
||
|
|
||
|
# Default values
|
||
|
DB_NAME=${DB_NAME:-"oedb"}
|
||
|
DB_USER=${DB_USER:-"postgres"}
|
||
|
DB_HOST=${DB_HOST:-"localhost"}
|
||
|
NEW_USER=${NEW_USER:-"cipherbliss"}
|
||
|
NEW_PASSWORD=${NEW_PASSWORD:-"tralalahihou"}
|
||
|
|
||
|
# Use POSTGRES_PASSWORD for database authentication
|
||
|
export PGPASSWORD="${POSTGRES_PASSWORD}"
|
||
|
|
||
|
# Check if running as root
|
||
|
if [ "$(id -u)" -eq 0 ]; then
|
||
|
# Install PostgreSQL if not already installed
|
||
|
if ! command -v psql &> /dev/null; then
|
||
|
echo "PostgreSQL not found. Installing..."
|
||
|
apt-get update
|
||
|
apt-get install -y postgresql postgresql-contrib postgis
|
||
|
|
||
|
# Start PostgreSQL service
|
||
|
service postgresql start
|
||
|
|
||
|
echo "PostgreSQL installed successfully."
|
||
|
else
|
||
|
echo "PostgreSQL is already installed."
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
# PGPASSWORD is already set at the top of the script
|
||
|
|
||
|
# Check if the specified user exists
|
||
|
if ! psql -h $DB_HOST -U $DB_USER -tAc "SELECT 1 FROM pg_roles WHERE rolname='$NEW_USER'" | grep -q 1; then
|
||
|
echo "Creating user '$NEW_USER'..."
|
||
|
psql -h $DB_HOST -U $DB_USER -c "CREATE USER $NEW_USER WITH PASSWORD '$NEW_PASSWORD';"
|
||
|
psql -h $DB_HOST -U $DB_USER -c "ALTER USER $NEW_USER WITH SUPERUSER;"
|
||
|
echo "User '$NEW_USER' created with superuser privileges."
|
||
|
fi
|
||
|
|
||
|
# Check if database exists
|
||
|
echo "Checking if database '$DB_NAME' exists..."
|
||
|
if psql -h $DB_HOST -U $DB_USER -lqt | cut -d \| -f 1 | grep -qw $DB_NAME; then
|
||
|
echo "Database '$DB_NAME' already exists."
|
||
|
|
||
|
# Grant privileges to the new user even if database already exists
|
||
|
echo "Granting privileges on database '$DB_NAME' to user '$NEW_USER'..."
|
||
|
psql -h $DB_HOST -U $DB_USER -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $NEW_USER;"
|
||
|
|
||
|
# Grant privileges on all tables to the new user
|
||
|
echo "Granting privileges on all tables to user '$NEW_USER'..."
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $NEW_USER;"
|
||
|
|
||
|
# Explicitly grant permissions on the events table
|
||
|
echo "Explicitly granting permissions on the events table to user '$NEW_USER'..."
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON TABLE events TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON TABLE events_deleted TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON TABLE geo TO $NEW_USER;"
|
||
|
else
|
||
|
echo "Database '$DB_NAME' does not exist. Creating..."
|
||
|
|
||
|
# Create database
|
||
|
createdb -h $DB_HOST -U $DB_USER $DB_NAME
|
||
|
|
||
|
if [ $? -eq 0 ]; then
|
||
|
echo "Database '$DB_NAME' created successfully."
|
||
|
|
||
|
# Grant privileges to the new user
|
||
|
echo "Granting privileges on database '$DB_NAME' to user '$NEW_USER'..."
|
||
|
psql -h $DB_HOST -U $DB_USER -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $NEW_USER;"
|
||
|
|
||
|
# Run setup.sql
|
||
|
echo "Running setup.sql to initialize the database schema..."
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -f setup.sql
|
||
|
|
||
|
# Grant privileges on all tables to the new user
|
||
|
echo "Granting privileges on all tables to user '$NEW_USER'..."
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "ALTER DATABASE $DB_NAME OWNER TO $NEW_USER;"
|
||
|
|
||
|
# Explicitly grant permissions on the events table
|
||
|
echo "Explicitly granting permissions on the events table to user '$NEW_USER'..."
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON TABLE events TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON TABLE events_deleted TO $NEW_USER;"
|
||
|
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON TABLE geo TO $NEW_USER;"
|
||
|
|
||
|
if [ $? -eq 0 ]; then
|
||
|
echo "Database schema initialized successfully."
|
||
|
else
|
||
|
echo "Error: Failed to initialize database schema."
|
||
|
exit 1
|
||
|
fi
|
||
|
else
|
||
|
echo "Error: Failed to create database '$DB_NAME'."
|
||
|
exit 1
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
echo "Database setup completed successfully."
|
||
|
./check_tables.sh
|
||
|
|
||
|
exit 0
|