#!/bin/bash
# Initialize pg_hba.conf for external connections

set -e

echo "Configuring PostgreSQL for external connections..."

# Create custom pg_hba.conf that allows password authentication from all hosts
cat > /var/lib/postgresql/pg_hba_custom.conf <<EOF
# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     trust
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5
host    all             all             ::/0                    md5
EOF

# Update postgresql.conf to use custom hba file
echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf
echo "host all all ::/0 md5" >> /var/lib/postgresql/data/pg_hba.conf

echo "PostgreSQL configured for external connections"
