检查PostgreSQL服务状态
确保服务正在运行:sudo systemctl status postgresql
,未运行则启动:sudo systemctl start postgresql
。
配置PostgreSQL允许远程连接
/etc/postgresql/[版本]/main/postgresql.conf
,设置listen_addresses = '*'
以允许所有IP连接。/etc/postgresql/[版本]/main/pg_hba.conf
,添加规则host all all 0.0.0.0/0 md5
(允许所有IP通过密码认证),重启服务:sudo systemctl restart postgresql
。检查防火墙设置
确保允许5432端口(默认):sudo ufw allow 5432/tcp
,并确认防火墙状态:sudo ufw status
。
验证连接参数
查看日志定位问题
/var/log/postgresql/postgresql-[版本]-main.log
。/var/log/pgadmin/pgadmin4.log
,根据错误信息进一步排查。其他高级操作
注意:操作前建议备份配置文件,修改后需重启服务生效。若涉及生产环境,需谨慎处理权限和防火墙规则。