-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_bootstrap.sh
More file actions
35 lines (28 loc) · 1.28 KB
/
post_bootstrap.sh
File metadata and controls
35 lines (28 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
set -e
# Called by Patroni after cluster bootstrap (bootstrap.post_bootstrap)
# First argument is the superuser connection string
CONN="${1}"
echo "post_bootstrap: Running post-bootstrap configuration..."
echo "post_bootstrap: POSTGRES_DB=${POSTGRES_DB:-postgres}"
# Escape password for SQL single-quote literal (' -> '')
# Passwords containing '$' are already rejected by entrypoint.sh validation
ESCAPED_PWD=$(printf '%s' "${POSTGRES_SUPERUSER_PASSWORD}" | sed "s/'/''/g")
# Create admin role (idempotent) and set password
PGPASSWORD="${POSTGRES_SUPERUSER_PASSWORD}" psql "${CONN}" -c "
DO \$\$ BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'admin') THEN
CREATE ROLE admin WITH LOGIN CREATEROLE CREATEDB;
END IF;
END \$\$;
ALTER ROLE admin WITH PASSWORD '${ESCAPED_PWD}';
"
echo "post_bootstrap: admin role ready"
# Create application database if different from the built-in 'postgres'
if [ "${POSTGRES_DB:-postgres}" != "postgres" ]; then
echo "post_bootstrap: Creating database '${POSTGRES_DB}'..."
PGPASSWORD="${POSTGRES_SUPERUSER_PASSWORD}" psql "${CONN}" \
-c "CREATE DATABASE \"${POSTGRES_DB}\" OWNER admin;" 2>/dev/null \
|| echo "post_bootstrap: Database '${POSTGRES_DB}' already exists, skipping."
fi
echo "post_bootstrap: Complete"