PGAdmin4是一款PostgreSQL數(shù)據(jù)庫(kù)的官方客戶端(PostgreSQL數(shù)據(jù)庫(kù)專用圖形化管理工具)。采用B/S架構(gòu)設(shè)計(jì),服務(wù)器端模式可以實(shí)現(xiàn)集中部署,用戶通過(guò)瀏覽器即可實(shí)現(xiàn)管理個(gè)人建立的數(shù)據(jù)庫(kù),即安全又便于使用;功能強(qiáng)大且支持中文、英文等多種語(yǔ)言。
本方案基于CentOS8系統(tǒng)設(shè)計(jì),建議在RedHat/CentOS系統(tǒng)中使用。
1.PGAdmin4的安裝
1、下載并安裝Extra Packages for Enterprise Linux(EPEL)的yum源配置文件??梢詮腃entOS官方y(tǒng)um源安裝,也可以使用國(guó)內(nèi)/本地/私有yum源安裝。本例使用了阿里云的yum源。
[centos@host ~]$ sudo dnf install https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
2、打開PostgreSQL官方網(wǎng)站下載頁(yè)面【https://www.postgresql.org/download/linux/】,選擇PostgreSQL適配的操作系統(tǒng)版本。

3、選擇PostgreSQL版本,適配平臺(tái)和架構(gòu)。

4、下載并安裝PostgreSQL官方y(tǒng)um源配置文件。
[centos@host ~]$ sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
技巧:PostgreSQL官方的yum源配置文件提供了PostgreSQL12/11/10/9.6/9.5/9.4共6個(gè)版本的配置信息,一般情況下我們只使用計(jì)劃安裝版本的配置信息,禁用不需要的配置信息可以提高下載速度。本例計(jì)劃安裝12版,可以禁用11/10/9.6/9.5/9.4版的配置信息。
[centos@host? ~]$ sudo dnf config-manager --disable pgdg11
[centos@host? ~]$ sudo dnf config-manager --disable pgdg10
[centos@host? ~]$ sudo dnf config-manager --disable pgdg96
[centos@host? ~]$ sudo dnf config-manager --disable pgdg96
[centos@host? ~]$ sudo dnf config-manager --disable pgdg94
注意:如果使用本地或私有yum源,可忽略第1-3步,直接獲取本地/私有yum源的配置文件使用即可。有關(guān)如何進(jìn)行yum源的本地化/私有化,請(qǐng)閱讀文章《RedHat/CentOS8 【國(guó)內(nèi)/本地/私有 YUM 源】制作和使用》,文章地址【http://www.itdecent.cn/p/68db74388600】。
5、通過(guò)PostgreSQL官方y(tǒng)um源安裝PGAdmin4客戶端程序。
[centos@host? ~]$ sudo dnf install pgadmin4
注意:PGAdmin4客戶端通過(guò) Apache Httpd 程序發(fā)布,自動(dòng)安裝Httpd。Httpd程序安裝目錄是"/usr/sbin",Httpd程序配置目錄是"/etc/httpd",PGAdmin4客戶端配置目錄是"/usr/lib/python3.6/site-packages/pgadmin4-web"。
2.PGAdmin4的配置
1、設(shè)置Httpd的用于啟動(dòng)PGAdmin4的虛擬目錄配置文件。
位于程序配置目錄"/etc/httpd/conf.d"下的"pgadmin4.conf.sample"文件是用于PGAdmin4虛擬目錄的配置文件,在PGAdmin4安裝時(shí)已經(jīng)被創(chuàng)建??截惒⒅孛@個(gè)文件為"pgadmin4.conf"并設(shè)置。
1)拷貝并重命名配置文件??截惒⒅孛?/etc/httpd/conf.d/pgadmin4.conf.sample"到"/etc/httpd/conf.d/pgadmin4.conf":
[centos@host? ~]$ sudo cp /etc/httpd/conf.d/pgadmin4.conf.sample /etc/httpd/conf.d/pgadmin4.conf
2)使用文本編輯器打開"/etc/httpd/conf.d/pgadmin4.conf"文件:
[centos@host? ~]$ sudo gedit /etc/httpd/conf.d/pgadmin4.conf
設(shè)置文件內(nèi)容如下(默認(rèn)已設(shè)置完成,只需要驗(yàn)證即可):

2、啟動(dòng)Httpd服務(wù),并設(shè)置Httpd服務(wù)器為開機(jī)自啟動(dòng)狀態(tài)。
[centos@host? ~]$ sudo systemctl enable httpd
[centos@host? ~]$ sudo systemctl start httpd
正常啟動(dòng)后,查詢啟動(dòng)狀態(tài):
[centos@host? ~]$ sudo systemctl status httpd
查詢結(jié)果如下圖,表示服務(wù)正常:

3、修改PGAdmin4初始化配置文件,指定PGAdmin4啟動(dòng)資源目錄。
使用文本編輯器打開"/usr/lib/python3.6/site-packages/pgadmin4-web/config_distro.py"文件:
[centos@host? ~]$ sudo gedit /usr/lib/python3.6/site-packages/pgadmin4-web/config_distro.py
在文件中追加以下內(nèi)容并保存:
HELP_PATH = '/usr/share/doc/pgadmin4-docs/en_US/html'
UPGRADE_CHECK_ENABLED = False
LOG_FILE = '/var/log/pgadmin4/pgadmin4.log'
SQLITE_PATH = '/var/lib/pgadmin4/pgadmin4.db'
SESSION_DB_PATH = '/var/lib/pgadmin4/sessions'
STORAGE_DIR = '/var/lib/pgadmin4/storage'
SERVER_MODE = True
4、執(zhí)行PGAdmin4初始化腳本,設(shè)置登錄賬戶的用戶名(郵箱格式)/口令,完成初始化。
[centos@host? ~]$ sudo python3? /usr/lib/python3.6/site-packages/pgadmin4-web/setup.py
初始化過(guò)程如下:

初始化工作會(huì)生成PGAdmin4啟動(dòng)所需資源的存儲(chǔ)目錄,登錄賬戶用戶名和口令。
5、修改PGAdmin4啟動(dòng)資源目錄的所有者用戶和組為Httpd服務(wù)的管理用戶"apache"。
[centos@host? ~]$ sudo chown -R apache:apache /var/lib/pgadmin4
[centos@host? ~]$ sudo chown -R apache:apache /var/log/pgadmin4
6、在SELinux模式運(yùn)行時(shí),設(shè)置自定義啟動(dòng)資源目錄的安全標(biāo)記和安全策略。
[centos@host? ~]$ sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/lib/pgadmin4(/.*)?"
[centos@host? ~]$ sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/log/pgadmin4(/.*)?"
[centos@host? ~]$ sudo restorecon -Rv /var/lib/pgadmin4/
[centos@host? ~]$ sudo restorecon -Rv /var/log/pgadmin4/
[centos@host? ~]$ sudo setsebool -P httpd_can_network_connect 1
7、重新啟動(dòng)Httpd服務(wù)。
[centos@host? ~]$ sudo systemctl restart httpd
8、設(shè)置防火墻端口(CentOS8默認(rèn)安裝firewall防火墻),允許"80"端口(Httpd默認(rèn)端口)訪問(wèn)服務(wù)器。
[centos@host? ~]$ sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
[centos@host? ~]$ sudo firewall-cmd --reload
3.PGAdmin4的使用
在瀏覽器中輸入"http://ip/pgadmin4"或 "http://localhost/pgadmin4"來(lái)訪問(wèn)PGAdmin4客戶端:

