Debian下配置PostgreSQL网络需修改配置文件并调整防火墙,步骤如下:
安装PostgreSQL
sudo apt update && sudo apt install postgresql postgresql-contrib
修改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用密码连接)。重启服务
sudo systemctl restart postgresql
配置防火墙(若使用ufw)
sudo ufw allow 5432/tcp
sudo ufw reload
验证连接
从远程主机使用psql
测试连接:
psql -h <服务器IP> -U <用户名> -d <数据库名>
注意:生产环境建议限制listen_addresses
为特定IP,避免使用0.0.0.0/0
,并启用SSL加密。