1. 程式人生 > >Linux 伺服器部署 PgAdmin 4 Server

Linux 伺服器部署 PgAdmin 4 Server

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.