Skip to content

PostgreSQL 防火墙设置

PostgreSQL 端口配置

默认端口

PostgreSQL 默认监听 5432 端口,可通过 postgresql.conf 修改:

txt
# 修改 PostgreSQL 监听端口
port = 5432

验证监听状态

bash
# 查看 PostgreSQL 监听端口
netstat -tuln | grep 5432
ss -tuln | grep 5432
lsof -i :5432

Linux 防火墙配置

1. iptables 配置

bash
# 允许来自特定IP的连接
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 5432 -j ACCEPT

# 允许来自本地的连接
iptables -A INPUT -p tcp -s 127.0.0.1 --dport 5432 -j ACCEPT

# 拒绝其他所有IP访问
iptables -A INPUT -p tcp --dport 5432 -j DROP

# 保存规则(CentOS/RHEL)
iptables-save > /etc/sysconfig/iptables

# 保存规则(Debian/Ubuntu)
iptables-save > /etc/iptables/rules.v4

2. ufw 配置

bash
# 安装 ufw
sudo apt-get install ufw

# 允许来自特定IP段的连接
sudo ufw allow from 192.168.1.0/24 to any port 5432 proto tcp

# 允许来自本地的连接
sudo ufw allow from 127.0.0.1 to any port 5432 proto tcp

# 启用 ufw
sudo ufw enable

# 查看状态
sudo ufw status verbose

3. firewalld 配置

bash
# 允许来自特定IP段的连接
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="5432" accept'

# 允许来自本地的连接
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="127.0.0.1" port protocol="tcp" port="5432" accept'

# 重新加载配置
sudo firewall-cmd --reload

# 查看状态
sudo firewall-cmd --list-all

Windows 防火墙配置

1. 图形界面配置

  1. 打开「控制面板」→「系统和安全」→「Windows Defender 防火墙」
  2. 点击「高级设置」
  3. 选择「入站规则」→「新建规则」
  4. 选择「端口」→「下一步」
  5. 选择「TCP」,输入「5432」→「下一步」
  6. 选择「允许连接」→「下一步」
  7. 选择适用的网络类型→「下一步」
  8. 输入规则名称,如「PostgreSQL 5432」→「完成」

2. 命令行配置

powershell
# 允许特定IP访问
New-NetFirewallRule -DisplayName "PostgreSQL Allow Specific IP" -Direction Inbound -Protocol TCP -LocalPort 5432 -RemoteAddress 192.168.1.0/24 -Action Allow

# 允许本地访问
New-NetFirewallRule -DisplayName "PostgreSQL Allow Local" -Direction Inbound -Protocol TCP -LocalPort 5432 -RemoteAddress 127.0.0.1 -Action Allow

# 查看防火墙规则
Get-NetFirewallRule -DisplayName *PostgreSQL*

云平台防火墙配置

1. AWS Security Groups

bash
# 使用 AWS CLI 创建安全组规则
aws ec2 authorize-security-group-ingress \
    --group-id sg-12345678 \
    --protocol tcp \
    --port 5432 \
    --cidr 192.168.1.0/24

2. 阿里云安全组

bash
# 使用阿里云 CLI 创建安全组规则
aliyun ecs AuthorizeSecurityGroup \
    --SecurityGroupId sg-12345678 \
    --IpProtocol tcp \
    --PortRange 5432/5432 \
    --SourceCidrIp 192.168.1.0/24

3. 腾讯云安全组

bash
# 使用腾讯云 CLI 创建安全组规则
tccli vpc CreateSecurityGroupPolicies \
    --SecurityGroupId sg-12345678 \
    --SecurityGroupPolicySet '{"Ingress":[{"Protocol":"TCP","Port":"5432","CidrBlock":"192.168.1.0/24","Action":"ACCEPT"}]}'

防火墙安全策略建议

1. 最小权限原则

bash
# 不推荐:允许所有IP访问
# iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

# 推荐:仅允许必要的IP访问
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 5432 -j ACCEPT
iptables -A INPUT -p tcp -s 10.0.0.0/8 --dport 5432 -j ACCEPT
iptables -A INPUT -p tcp --dport 5432 -j DROP

2. 结合 pg_hba.conf

防火墙和 pg_hba.conf 配合使用,实现多层防护:

txt
# pg_hba.conf 配置
host    all             all             192.168.1.0/24          md5
host    all             all             10.0.0.0/8              md5
host    all             all             0.0.0.0/0               reject

3. 使用 VPN 或 SSH 隧道

对于远程管理,推荐使用 VPN 或 SSH 隧道,而不是直接开放防火墙:

bash
# 使用 SSH 隧道连接 PostgreSQL
ssh -L 5432:localhost:5432 user@remote-server
psql -h localhost -p 5432 -U postgres

4. 定期审计防火墙规则

bash
# 审计 iptables 规则
iptables -L -n -v

# 审计 ufw 规则
sudo ufw status verbose

# 审计 Windows 防火墙规则
Get-NetFirewallRule -DisplayName *PostgreSQL* | Format-List

防火墙测试

1. 从客户端测试连接

bash
# 使用 telnet 测试端口连通性
telnet postgres-server 5432

# 使用 nc 测试端口连通性
nc -zv postgres-server 5432

# 使用 psql 测试数据库连接
psql -h postgres-server -p 5432 -U postgres -d postgres -c "SELECT 1;"

2. 从服务器测试监听状态

bash
# 查看 PostgreSQL 进程
ps aux | grep postgres

# 查看网络连接
netstat -an | grep 5432
ss -an | grep 5432

常见问题(FAQ)

Q1:防火墙配置后无法连接数据库,如何排查?

A1:

  1. 检查 PostgreSQL 是否正常运行:systemctl status postgresql
  2. 检查 PostgreSQL 是否监听正确的端口和IP:netstat -tuln | grep 5432
  3. 检查防火墙规则是否正确:iptables -L -n -vsudo ufw status verbose
  4. 检查 pg_hba.conf 配置是否允许该IP访问
  5. 从客户端测试端口连通性:telnet postgres-server 5432

Q2:如何允许多个IP段访问 PostgreSQL?

A2:为每个IP段添加单独的防火墙规则:

bash
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 5432 -j ACCEPT
iptables -A INPUT -p tcp -s 10.0.0.0/8 --dport 5432 -j ACCEPT
iptables -A INPUT -p tcp -s 172.16.0.0/12 --dport 5432 -j ACCEPT
iptables -A INPUT -p tcp --dport 5432 -j DROP

Q3:防火墙规则变更后需要重启服务吗?

A3:

  • iptables:规则立即生效,无需重启
  • ufw:规则立即生效,无需重启
  • firewalld:需要执行 firewall-cmd --reload
  • Windows 防火墙:规则立即生效,无需重启
  • PostgreSQL:防火墙变更不影响数据库服务,无需重启

Q4:如何临时开放 PostgreSQL 端口进行维护?

A4:

bash
# iptables 临时开放
iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

# 维护完成后移除规则
iptables -D INPUT -p tcp --dport 5432 -j ACCEPT

Q5:如何使用防火墙限制连接速率?

A5:使用 iptables 的 limit 模块限制连接速率:

bash
# 限制每秒最多10个连接,最多突发50个连接
iptables -A INPUT -p tcp --dport 5432 -m limit --limit 10/s --limit-burst 50 -j ACCEPT
iptables -A INPUT -p tcp --dport 5432 -j DROP

Q6:防火墙日志如何配置?

A6:

bash
# iptables 配置日志
iptables -A INPUT -p tcp --dport 5432 -j LOG --log-prefix "[PostgreSQL DROP] " --log-level 6
iptables -A INPUT -p tcp --dport 5432 -j DROP

# 查看日志
cat /var/log/kern.log | grep PostgreSQL