代码改变世界

pgAdmin的Desktop Mode设置允许远程访问

2020-12-09 10:56  潇湘隐者  阅读(2453)  评论(0编辑  收藏  举报

有时候因为一些安全设置或权限问题,不允许用户直接访问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了。

 

clip_image001

 

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

 

 

 

clip_image002

 

虽然通过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