Some checks failed
Build and Deploy Docs / build-and-deploy (push) Has been cancelled
- Created logs/ structure (sessions, operations, incidents) - Added session-start/log/end scripts - Installed Git hooks for auto-logging commits/pushes - Added shell integration for zsh - Created CHANGELOG.md - Documented today's session (2026-01-10)
70 lines
1.9 KiB
Bash
70 lines
1.9 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeo pipefail
|
|
|
|
# usage: file_env VAR [DEFAULT]
|
|
file_env() {
|
|
local var="$1"
|
|
local fileVar="${var}_FILE"
|
|
local def="${2:-}"
|
|
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
|
|
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
|
|
exit 1
|
|
fi
|
|
local val="$def"
|
|
if [ "${!var:-}" ]; then
|
|
val="${!var}"
|
|
elif [ "${!fileVar:-}" ]; then
|
|
val="$(< "${!fileVar}")"
|
|
fi
|
|
export "$var"="$val"
|
|
unset "$fileVar"
|
|
}
|
|
|
|
# Setup environment variables
|
|
file_env 'POSTGRES_PASSWORD'
|
|
file_env 'POSTGRES_USER' 'postgres'
|
|
file_env 'POSTGRES_DB' "$POSTGRES_USER"
|
|
file_env 'POSTGRES_INITDB_ARGS'
|
|
|
|
# Initialize database if needed
|
|
if [ ! -s "$PGDATA/PG_VERSION" ]; then
|
|
echo "Initializing database..."
|
|
|
|
/usr/lib/postgresql/16/bin/initdb --username="$POSTGRES_USER" --pwfile=<(echo "$POSTGRES_PASSWORD") $POSTGRES_INITDB_ARGS
|
|
|
|
# Configure pg_hba.conf for network access
|
|
{
|
|
echo
|
|
echo "host all all all scram-sha-256"
|
|
} >> "$PGDATA/pg_hba.conf"
|
|
|
|
# Start temporary server for setup
|
|
/usr/lib/postgresql/16/bin/pg_ctl -D "$PGDATA" -w start -o "-c listen_addresses=''" || exit 1
|
|
|
|
# Create database if needed
|
|
if [ "$POSTGRES_DB" != 'postgres' ]; then
|
|
/usr/lib/postgresql/16/bin/psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres <<-EOSQL
|
|
CREATE DATABASE "$POSTGRES_DB";
|
|
EOSQL
|
|
fi
|
|
|
|
# Run init scripts if present
|
|
if [ -d /docker-entrypoint-initdb.d ]; then
|
|
for f in /docker-entrypoint-initdb.d/*; do
|
|
case "$f" in
|
|
*.sh) echo "$0: running $f"; . "$f" ;;
|
|
*.sql) echo "$0: running $f"; /usr/lib/postgresql/16/bin/psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f" ;;
|
|
*) echo "$0: ignoring $f" ;;
|
|
esac
|
|
done
|
|
fi
|
|
|
|
# Stop temporary server
|
|
/usr/lib/postgresql/16/bin/pg_ctl -D "$PGDATA" -m fast -w stop
|
|
|
|
echo "Database initialization complete."
|
|
fi
|
|
|
|
# Start PostgreSQL
|
|
exec /usr/lib/postgresql/16/bin/postgres "$@"
|