#!/bin/bash echo "配置PostgreSQL允许Docker网络访问..." # 找到pg_hba.conf PG_HBA=$(find /var/lib/postgresql /etc/postgresql -name pg_hba.conf 2>/dev/null | head -1) if [ -z "$PG_HBA" ]; then echo "未找到pg_hba.conf" exit 1 fi echo "找到配置文件: $PG_HBA" # 备份 cp $PG_HBA ${PG_HBA}.bak # 添加Docker网络访问权限 echo "host all all 172.0.0.0/8 md5" >> $PG_HBA echo "配置已更新" # 重启PostgreSQL echo "重启PostgreSQL..." systemctl restart postgresql # 测试 echo "" echo "测试连接:" PGPASSWORD=gongxiangpan123 psql -U gongxiangpan -h 172.21.0.1 -p 5432 -d lobechat -c "SELECT current_database();" echo "" echo "完成!"