You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置PostgreSQL仅允许私有网络IP远程连接?

配置PostgreSQL允许指定私有IP段远程连接

两种配置方案可选:

方案1:用单个CIDR块覆盖整个IP范围

你给出的1.1.10.0-255到1.1.13.0-255四个连续网段,可合并为一个CIDR表示:1.1.10.0/22,这个块刚好包含1.1.10.0至1.1.13.255的所有IP。

先打开配置文件:

sudo nano /etc/postgresql/14/main/pg_hba.conf

替换原有的0.0.0.0/0规则,添加这条配置:

host        all         all         1.1.10.0/22   scram-sha-256

方案2:分四条规则单独配置每个网段

如果不想合并网段,也可以为每个单独的0-255网段添加规则(每个网段对应/24的CIDR):

host        all         all         1.1.10.0/24   scram-sha-256
host        all         all         1.1.11.0/24   scram-sha-256
host        all         all         1.1.12.0/24   scram-sha-256
host        all         all         1.1.13.0/24   scram-sha-256

额外注意事项

别忘了修改postgresql.conf的监听地址,让PostgreSQL能接收远程请求:

sudo nano /etc/postgresql/14/main/postgresql.conf

找到listen_addresses项,修改为:

listen_addresses = '*'

所有配置修改完成后,重启PostgreSQL服务生效:

sudo systemctl restart postgresql

内容的提问来源于stack exchange,提问作者Mainland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 05:00:08