#!/bin/bash set -e echo "=== PostgreSQL + PostGIS Setup ===" # Check if running as root or with sudo if [ "$EUID" -ne 0 ]; then echo "This script requires root privileges. Please run with sudo." exit 1 fi # Install PostgreSQL and PostGIS echo "Installing PostgreSQL and PostGIS..." if command -v apt-get &> /dev/null; then # Debian/Ubuntu apt-get update apt-get install -y postgresql postgresql-contrib postgis postgresql-postgis elif command -v yum &> /dev/null; then # RHEL/CentOS/Amazon Linux amazon-linux-extras enable postgresql13 yum install -y postgresql-server postgresql-contrib postgis postgresql-setup initdb else echo "Unsupported package manager. Please install PostgreSQL manually." exit 1 fi # Start PostgreSQL systemctl enable postgresql systemctl start postgresql # Create database and user echo "Creating database and user..." sudo -u postgres psql <> "$PG_HBA" echo "host all iom 127.0.0.1/32 md5" >> "$PG_HBA" echo "host all iom ::1/128 md5" >> "$PG_HBA" # Restart PostgreSQL systemctl restart postgresql echo "=== PostgreSQL Setup Complete ===" echo "Database: iom" echo "User: iom" echo "Test connection: psql -U iom -d iom -h localhost"