65 lines
1.9 KiB
Bash
65 lines
1.9 KiB
Bash
|
|
#!/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 <<EOF
|
||
|
|
CREATE USER iom WITH PASSWORD 'iom_s…';
|
||
|
|
CREATE DATABASE iom OWNER iom;
|
||
|
|
GRANT ALL PRIVILEGES ON DATABASE iom TO iom;
|
||
|
|
EOF
|
||
|
|
|
||
|
|
# Enable PostGIS
|
||
|
|
echo "Enabling PostGIS..."
|
||
|
|
sudo -u postgres psql -d iom <<EOF
|
||
|
|
CREATE EXTENSION IF NOT EXISTS postgis;
|
||
|
|
CREATE EXTENSION IF NOT EXISTS postgis_topology;
|
||
|
|
EOF
|
||
|
|
|
||
|
|
# Apply schema
|
||
|
|
echo "Applying schema..."
|
||
|
|
sudo -u postgres psql -d iom -f /home/bernt/.openclaw/workspace/iom/database/schema.sql
|
||
|
|
|
||
|
|
# Configure pg_hba.conf for local access
|
||
|
|
echo "Configuring access..."
|
||
|
|
PG_HBA=$(sudo -u postgres psql -t -P format=unaligned -c 'SHOW hba_file;')
|
||
|
|
echo "local all iom md5" >> "$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"
|