在Linux上配置pgAdmin网络访问需从服务端和客户端两方面操作,核心步骤如下:
修改PostgreSQL监听设置
编辑 postgresql.conf
(路径:/etc/postgresql/<版本>/main/
):
listen_addresses = '*' # 允许所有IP连接(或指定具体IP)
port = 5432 # 默认端口(可自定义)
编辑 pg_hba.conf
,添加远程访问规则:
host all all 0.0.0.0/0 md5 # 允许所有IP通过密码认证
重启PostgreSQL:
sudo systemctl restart postgresql
配置pgAdmin监听(可选)
若需直接通过pgAdmin服务端访问,编辑 pgadmin.conf
(路径:/etc/pgadmin/
):
listen_address = '0.0.0.0' # 监听所有网络接口
port = 5050 # 默认端口(可自定义)
重启pgAdmin服务:
sudo systemctl restart pgadmin4
开放防火墙端口
sudo firewall-cmd --permanent --add-port=5432/tcp # PostgreSQL端口
sudo firewall-cmd --permanent --add-port=5050/tcp # pgAdmin端口(若需)
sudo firewall-cmd --reload
sudo ufw allow 5432/tcp
sudo ufw allow 5050/tcp
通过pgAdmin客户端连接
打开pgAdmin,在左侧“服务器”节点右键选择“创建→服务器”,填写:
测试连接
保存后双击服务器节点,若配置正确可看到数据库列表。
pg_hba.conf
中的IP范围(如仅允许可信IP),避免使用 0.0.0.0/0
。postgresql.conf
和 pgadmin.conf
中配置证书)。参考来源: