有時候因為一些安全設置或權限問題,不允許用戶直接訪問PostgreSQL數據庫,但是Support系統的用戶必須要訪問數據庫. 那怎么辦呢? 在一台可以訪問PostgreSQL的服務器上安裝pgAdmin,然后讓用戶使用瀏覽器遠程訪問pgAdmin,訪問PostgreSQL數據庫, 那么pgAdmin如何設置允許遠程訪問呢?其實pgAdmin有Desktop Mode和Server Mode兩種模式,對於Server Mode的話,這個根本不是問題。
下面介紹Desktop Mode的pgAdmin如何允許遠程訪問,這個是在對pgAdmin這個工具不熟悉的情況下的一個嘗試。很多時候,我們都像一個蹣跚學步的小孩一樣,需要時間和嘗試才會熟練的掌握一款工具!過程中總有磕磕碰碰。
下面測試環境為Windows Server 2019 Datacenter版本,在pgAdmin的安裝目錄,例如,C:\Program Files\pgAdmin 4\v4\web目錄下(這里是默認安裝,根據實際情況找到安裝目錄)config.py文件, 找到DEFAULT_SERVER參數,將其值修改為'0.0.0.0'后
# This option allows the user to host the application on a LAN
# Default hosting is on localhost (DEFAULT_SERVER='localhost').
# To host pgAdmin4 over LAN set DEFAULT_SERVER='0.0.0.0' (or a specific
# adaptor address.
#
# NOTE: This is NOT recommended for production use, only for debugging
# or testing. Production installations should be run as a WSGI application
# behind Apache HTTPD.
DEFAULT_SERVER = '0.0.0.0'
# The default port on which the app server will listen if not set in the
# environment by the runtime
DEFAULT_SERVER_PORT = 5050
退出並重新啟動pgAdmin后,找到右下角pgAdmin的圖標,選擇”Copy server URL“,就可以在遠程服務器使用類似下面的URL鏈接訪問pgAdmin了。
http://192.168.103.63:52846/?key=33962ea5-6319-4659-bdfa-6b9b233e6eb9
測試過程中發現,啟用遠程訪問pgAdmin后,不需要輸入master密碼了。后面查看官方文檔才知道,一般而言只有destop mode才有master password,但是修改參數DEFAULT_SERVER = '0.0.0.0'后,只有本機第一次登錄paAdmin才會需要master密碼,而其他用戶通過瀏覽器遠程訪問paAdmin的話,不需要輸入master密碼了。取而代之的是一長串key:33962ea5-6319-4659-bdfa-6b9b233e6eb9 當然這個key是變化的。每次重啟pgAdmin后就會重新生成一長串Key。
##########################################################################
# Master password is used to encrypt/decrypt saved server passwords
# Applicable for desktop mode only
##########################################################################
MASTER_PASSWORD_REQUIRED = True
雖然通過key這種方式也能達到一定的安全性,例如,如果你不知道key,使用下面鏈接是訪問不了系統的。
http://192.168.103.63:52846//browser/
它會提示下面錯誤信息,使用上面帶key的URL鏈接訪問過后,上面這種鏈接才能正常訪問。總之安全總是相對的。對於Desktop Mode的pgAdmin還是不建議開啟遠程訪問。
Unauthorized
The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.
參考資料:
https://www.pgadmin.org/faq/#2
https://www.pgadmin.org/docs/pgadmin4/development/server_deployment.html


