Debian上配置PostgreSQL网络需关注以下要点:
修改监听地址
编辑/etc/postgresql/<版本>/main/postgresql.conf,将listen_addresses设为'*'以允许所有IP连接(生产环境建议指定可信IP段)。
配置客户端认证
编辑/etc/postgresql/<版本>/main/pg_hba.conf,添加规则如host all all 0.0.0.0/0 md5(允许所有IP通过密码认证),或限制特定IP段。
重启服务生效
执行sudo systemctl restart postgresql使配置生效。
开放防火墙端口
若使用UFW,运行sudo ufw allow 5432/tcp;若用iptables,添加规则-A INPUT -p tcp --dport 5432 -j ACCEPT。
验证网络连通性
使用ping测试服务器网络可达性,通过sudo netstat -plnt | grep postgres确认PostgreSQL监听远程端口。
安全增强
trust认证,优先使用md5或scram-sha-256。max_connections避免资源耗尽。