#!/usr/bin/env bash . "$(cd "$(dirname "$0")/.." && pwd)/config/load.sh" # 在 47 初始化「控制面 PG」:独立数据目录 -pg + 专用 unit dshs-pg.service # 设计口径:控制面 DB 在 Manager 侧、仅 loopback、scram 认证。 set -uo pipefail PGDATA="${DSH_PG_DATA_DIR}" PGPORT=15432 DBPW="${DSHS_PG_PASSWORD:-dshs_cluster_2026}" PGVER=$(/usr/bin/postgres --version | grep -oE '[0-9]+' | head -1) echo " PG 版本: $(/usr/bin/postgres --version)" # 别让发行版的默认单元意外起来(我们用自己的 unit + 自己的数据目录) systemctl disable --now postgresql 2>/dev/null >/dev/null || true if [ ! -f "$PGDATA/PG_VERSION" ]; then install -d -o postgres -g postgres -m 700 "$PGDATA" su - postgres -c "/usr/bin/initdb -D $PGDATA -E UTF8 --locale=C.UTF-8 --auth-local=peer --auth-host=scram-sha-256" >/tmp/initdb.log 2>&1 \ && echo " ✓ initdb 完成" || { echo " ✗ initdb 失败"; tail -5 /tmp/initdb.log; exit 1; } cat >> "$PGDATA/postgresql.conf" < /etc/systemd/system/dshs-pg.service </dev/null 2>&1 sleep 3 echo " dshs-pg: $(systemctl is-active dshs-pg) 监听: $(ss -lntp 2>/dev/null | grep -c $PGPORT)" # 角色 + 库(幂等) su - postgres -c "/usr/bin/psql -p $PGPORT -tAc \"select 1 from pg_roles where rolname='dshs'\"" 2>/dev/null | grep -q 1 \ || su - postgres -c "/usr/bin/psql -p $PGPORT -c \"create role dshs login password '$DBPW'\"" >/dev/null 2>&1 su - postgres -c "/usr/bin/psql -p $PGPORT -tAc \"select 1 from pg_database where datname='dshs'\"" 2>/dev/null | grep -q 1 \ || su - postgres -c "/usr/bin/psql -p $PGPORT -c \"create database dshs owner dshs\"" >/dev/null 2>&1 echo " --- 连接自检(dshs 角色) ---" PGPASSWORD="$DBPW" /usr/bin/psql -h 127.0.0.1 -p $PGPORT -U dshs -d dshs -tAc "select current_user||'@'||current_database()||' pg='||version()" 2>&1 | head -1 | cut -c1-90 echo " 连接串(含密码,勿外传): postgres://dshs:$DBPW@127.0.0.1:$PGPORT/dshs"