PostgreSQL 使用率愈來愈高,可是好用的圖形化軟件,諸如 Navicat、DataGrip、HeidiSQL 都是客戶端軟件,對於訪問控制來講,比起 MySQL 的 phpMyAdmin 更加麻煩。與 pma 對標的 phpPgAdmin 自 2013 年停更,而且不支持 PostgreSQL 10,很是尷尬。好在 PgAdmin 4 提供了 Server Deploy 選項。本文以 Ubuntu Linux 爲例子,提供一個 PgAdmin 4 Server 的部署案例。php
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
sudo pip install virtualenvwrapper virtualenv pgadmin4 cd pgadmin4/ source bin/activate
最新的源碼包在 https://www.postgresql.org/ftp/pgadmin/pgadmin4/
請從這裏獲取下載地址。python
下面的代碼是以 3.5 爲藍本。web
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
python web/setup.py
這裏不出意外會報一個錯:sql
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。查閱源代碼:session
# 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
就行。刪除這幾行,而後替換爲:app
SERVER_MODE = True
後再來一次 python web/setup.py
,按提示輸入郵箱地址和密碼。post
python web/pgAdmin4.py
輸出:ui
Starting pgAdmin 4. Please navigate to http://localhost:5050 in your browser.
只能本機訪問,端口是5050。官方提供的方案是用 Apache 作 RP。若是嫌麻煩,在 web
目錄下建立 config_local.py
:postgresql
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.
。code