一 环境准备与版本差异
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpmsudo yum install -y postgresql13-serversudo /usr/pgsql-13/bin/postgresql-13-setup initdbsudo systemctl enable --now postgresql-13sudo dnf -qy module disable postgresqlsudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpmsudo dnf install -y postgresql16-server postgresql16-contribsudo /usr/pgsql-16/bin/postgresql-16-setup initdbsudo systemctl enable --now postgresql-16sudo firewall-cmd --permanent --add-port=5432/tcpsudo firewall-cmd --reload二 核心网络参数配置
listen_addresses = '*'port = 5432(默认)local all all peer(本地系统用户映射更安全)host all all 127.0.0.1/32 md5(密码认证)host all all 192.168.1.0/24 md5(按需替换为你的网段)host all all 0.0.0.0/0 md5(仅测试或严格限制来源时使用)sudo systemctl restart postgresql-13sudo systemctl restart postgresql-16三 防火墙与云安全组放行
sudo firewall-cmd --permanent --add-port=5432/tcpsudo firewall-cmd --reloadss -tnlp | grep :5432 或 netstat -tnlp | grep :5432psql -h <服务器IP> -U <用户名> -d <数据库> -p 5432四 用户与数据库准备
sudo -iu postgrespsql -c "ALTER USER postgres WITH PASSWORD 'StrongPass!123';"CREATE USER deploy_user WITH PASSWORD 'StrongPass!123' LOGIN CREATEDB;CREATE DATABASE app_db OWNER deploy_user;GRANT ALL PRIVILEGES ON DATABASE app_db TO deploy_user;psql -h <服务器IP> -U deploy_user -d app_db -p 5432五 安全加固与常见问题
pg_hba.conf 中的 0.0.0.0/0 替换为最小必要网段(如 192.168.1.0/24)。postgresql.conf 中配置 ssl = on 并部署服务器证书,客户端使用 sslmode=require。listen_addresses 与 port 正确并重启服务。pg_hba.conf 的网段、用户、数据库与 METHOD(如 md5/scram-sha-256)。firewalld/云安全组与云厂商安全策略是否放行 5432/tcp。listen_addresses = '*'。