在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
中配置證書)。參考來源: