以下是Debian系统上PostgreSQL的安全设置方法:
系统与软件更新
定期更新系统和PostgreSQL软件包,修复安全漏洞:
sudo apt update && sudo apt upgrade 用户与权限管理
postgres用户设置强密码:sudo -u postgres psql -c "ALTER USER postgres PASSWORD '强密码';" sudo -u postgres psql CREATE USER 应用用户 WITH PASSWORD '应用密码'; GRANT CONNECT ON DATABASE 应用库 TO 应用用户; 配置文件安全设置
/etc/postgresql/{version}/main/postgresql.conf,将listen_addresses设为localhost或特定IP,避免'*'。postgresql.conf中设置ssl=on,并指定证书路径;在pg_hba.conf中使用hostssl认证方式。pg_hba.conf中限制IP段和认证方式(如md5),示例:host all all 192.168.1.0/24 md5 # 仅允许指定IP段使用密码连接 防火墙与网络隔离
使用UFW等防火墙仅开放PostgreSQL端口(默认5432)给必要IP:
sudo ufw allow from 192.168.1.0/24 to any port 5432 sudo ufw reload 日志与监控
启用详细日志记录,定期分析异常行为:
# 在postgresql.conf中设置 log_statement = 'all' log_connections = true log_disconnections = true 其他安全措施
postgres数据库的远程访问,或删除不必要的数据库。pg_dump工具自动化备份。参考来源: