PostgreSQL 使用率越来越高,但是好用的图形化软件,诸如 Navicat、DataGrip、HeidiSQL 都是客户端软件,对于访问控制来说,比起 MySQL 的 phpMyAdmin 更加麻烦。与 pma 对标的 phpPgAdmin 自 2013 年停更,并且不支持 PostgreSQL 10,非常尴尬。好在 PgAdmin 4 提供了 Server Deploy 选项。本文以 Ubuntu Linux 为例子,提供一个 PgAdmin 4 Server 的部署案例。
安装 pip 安装包:
sudo apt-get install build-essential libssl-dev libffi-dev libgmp3-dev virtualenv python-pip libpq-dev python-dev
更新软件
sudo apt-get update
sudo apt-get dist-upgrade
安装 virtualenv 虚拟环境
sudo pip install virtualenvwrapper
virtualenv pgadmin4
cd pgadmin4/
source bin/activate
下载 PgAdmin4 源码包
最新的源码包在 https://www.postgresql.org/ftp/pgadmin/pgadmin4/
请从这里获取下载地址。
下面的代码是以 3.5 为蓝本。
wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v3.5/source/pgadmin4-3.5.tar.gz
tar xf pgadmin4-3.5.tar.gz
cd pgadmin4-3.5/
安装依赖库
sudo apt-get install libpq-dev
pip install -r requirements.txt
安装 PgAdmin4
python web/setup.py
这里不出意外会报一个错:
Traceback (most recent call last):
File "web/setup.py", line 15, in <module>
from pgadmin.model import db, Version, SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
File "/root/pgadmin4/pgadmin4-3.5/web/pgadmin/__init__.py", line 33, in <module>
from pgadmin.utils.session import create_session_interface, pga_unauthorised
File "/root/pgadmin4/pgadmin4-3.5/web/pgadmin/utils/session.py", line 26, in <module>
import config
File "/root/pgadmin4/pgadmin4-3.5/web/config.py", line 121, in <module>
if builtins.SERVER_MODE is None:
AttributeError: 'module' object has no attribute 'SERVER_MODE'
总而言之,因为没有 SERVER_MODE
属性导致 Crash。查阅源代码:
# The server mode determines whether or not we're running on a web server
# requiring user authentication, or desktop mode which uses an automatic
# default login.
#
# DO NOT DISABLE SERVER MODE IF RUNNING ON A WEBSERVER!!
#
# We only set SERVER_MODE if it's not already set. That's to allow the
# runtime to force it to False.
#
# NOTE: If you change the value of SERVER_MODE in an included config file,
# you may also need to redefine any values below that are derived
# from it, notably various paths such as LOG_FILE and anything
# using DATA_DIR.
if builtins.SERVER_MODE is None:
SERVER_MODE = True
else:
SERVER_MODE = builtins.SERVER_MODE
因为我们就是 Server Deploy,直接 SERVER_MODE = True
就行。删除这几行,然后替换为:
SERVER_MODE = True
后再来一次 python web/setup.py
,按提示输入邮箱地址和密码。
运行 PgAdmin4
python web/pgAdmin4.py
输出:
Starting pgAdmin 4. Please navigate to http://localhost:5050 in your browser.
只能本机访问,端口是5050。官方提供的方案是用 Apache 做 RP。如果嫌麻烦,在 web
目录下创建 config_local.py
:
from config import *
DEFAULT_SERVER = '0.0.0.0'
DEFAULT_SERVER_PORT = 9527
再次运行 PgAdmin4 即可通过 9527 端口直接访问了:
Starting pgAdmin 4. Please navigate to http://0.0.0.0:9527 in your browser.
```。