#!/bin/sh

# Update APK repositories
apk update

# Install PostgreSQL
apk add postgresql postgresql-client

# Create a new database directory
mkdir -p /var/lib/postgresql/data

# Initialize the database
su - postgres -c "initdb /var/lib/postgresql/data"

# Start PostgreSQL
su - postgres -c "pg_ctl start -D /var/lib/postgresql/data -l logfile"

# Create a new user and database
su - postgres -c "psql -c \"CREATE USER myuser WITH PASSWORD 'mypassword';\""
su - postgres -c "psql -c \"CREATE DATABASE mydb OWNER myuser;\""

# Enable remote connections (optional)
echo "host all  all    0.0.0.0/0  md5" >> /var/lib/postgresql/data/pg_hba.conf
echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf

# Restart PostgreSQL to apply changes
su - postgres -c "pg_ctl restart -D /var/lib/postgresql/data"
