63 lines
2.6 KiB
Bash
63 lines
2.6 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
# 在 47 初始化「控制面 PG」:独立数据目录 /var/lib/dshs-pg + 专用 unit dshs-pg.service
|
||
|
|
# 设计口径:控制面 DB 在 Manager 侧、仅 loopback、scram 认证。
|
||
|
|
set -uo pipefail
|
||
|
|
PGDATA=/var/lib/dshs-pg
|
||
|
|
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" <<CONF
|
||
|
|
|
||
|
|
# ── DSHS 控制面(2026-09-15 集群化切换)──
|
||
|
|
listen_addresses = '127.0.0.1'
|
||
|
|
port = $PGPORT
|
||
|
|
unix_socket_directories = '/var/run/postgresql'
|
||
|
|
max_connections = 100
|
||
|
|
shared_buffers = 128MB
|
||
|
|
CONF
|
||
|
|
chown postgres:postgres "$PGDATA/postgresql.conf"
|
||
|
|
echo " ✓ 已写入 listen=127.0.0.1 port=$PGPORT"
|
||
|
|
fi
|
||
|
|
|
||
|
|
cat > /etc/systemd/system/dshs-pg.service <<UNIT
|
||
|
|
[Unit]
|
||
|
|
Description=DSHS control-plane PostgreSQL (cluster mode)
|
||
|
|
After=network.target
|
||
|
|
|
||
|
|
[Service]
|
||
|
|
Type=notify
|
||
|
|
User=postgres
|
||
|
|
Group=postgres
|
||
|
|
ExecStart=/usr/bin/postgres -D $PGDATA
|
||
|
|
ExecReload=/bin/kill -HUP \$MAINPID
|
||
|
|
KillMode=mixed
|
||
|
|
TimeoutStopSec=30
|
||
|
|
Restart=on-failure
|
||
|
|
|
||
|
|
[Install]
|
||
|
|
WantedBy=multi-user.target
|
||
|
|
UNIT
|
||
|
|
|
||
|
|
systemctl daemon-reload
|
||
|
|
systemctl enable --now dshs-pg >/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"
|