PGAdmin4是一款PostgreSQL數(shù)據(jù)庫(kù)的官方客戶端(PostgreSQL數(shù)據(jù)庫(kù)專用圖形化管理工具)。采用B/S架構(gòu)設(shè)計(jì)贩汉,服務(wù)器端模式可以實(shí)現(xiàn)集中部署,用戶通過瀏覽器即可實(shí)現(xiàn)管理個(gè)人建立的數(shù)據(jù)庫(kù)锚赤,即安全又便于使用匹舞;功能強(qiáng)大且支持中文、英文等多種語言线脚。
本方案基于CentOS8系統(tǒng)設(shè)計(jì)赐稽,建議在RedHat/CentOS系統(tǒng)中使用。
1.PGAdmin4的安裝
1浑侥、下載并安裝Extra Packages for Enterprise Linux(EPEL)的yum源配置文件姊舵。可以從CentOS官方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.reibang.com/p/68db74388600】牍疏。
5、通過PostgreSQL官方y(tǒng)um源安裝PGAdmin4客戶端程序拨齐。
[centos@host? ~]$ sudo dnf install pgadmin4
注意:PGAdmin4客戶端通過 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
初始化過程如下:
初始化工作會(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ù)器兔甘。
[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"來訪問PGAdmin4客戶端: