#!/bin/bash
set -e

# Copy the compressed dump file
cp /tmp/dbbackup/dbbackup.sql.gz /tmp/

# Create draydex_read user
psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "CREATE USER draydex_read WITH PASSWORD 'draydex_read';"

# Restore the database
gunzip -c /tmp/dbbackup.sql.gz | psql -U "$POSTGRES_USER" -d "$POSTGRES_DB"

# Optional: Grant privileges if needed
psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $POSTGRES_USER;"