在 CentOS 8 上安装 pgAdmin Yum 存储库

pgAdmin 是一个流行的 Web 界面,用于 PostgreSQL 数据库的数据库管理。

尽管 PostgreSQL 官方 yum 存储库中也提供了 pgAdmin。
但它不适用于我们的 CentOS 8 服务器。

因此,我们正在从 pgAdmin 官方 yum 存储库安装 pgAdmin 的最新稳定版本。

为此,首先我们需要从 Linux 服务器中删除 PostgreSQL yum 存储库。

# dnf remove -y pgdg-redhat-repo

现在,在 Linux 操作系统中添加 pgAdmin 官方 yum 存储库。

# dnf install -y https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-1-1.noarch.rpm

我们还需要安装 EPEL (Extra Packages for Enterprise Linux) yum 存储库,因为 pgAdmin 需要一些标准 yum 存储库中没有的软件包。

使用 dnf 命令并安装 EPEL yum 存储库。

# dnf install -y epel-release

为新安装的 yum 存储库构建缓存。

# dnf makecache
CentOS-8 - AppStream                            2.1 kB/s | 4.3 kB     00:02
CentOS-8 - Base                                 3.7 kB/s | 3.9 kB     00:01
CentOS-8 - Extras                               768  B/s | 1.5 kB     00:02
Extra Packages for Enterprise Linux Modular 8 -  24 kB/s |  98 kB     00:04
Extra Packages for Enterprise Linux 8 - x86_64   92 kB/s | 8.3 MB     01:32
pgadmin4                                        1.1 kB/s | 2.9 kB     00:02
Metadata cache created.

安装 Postgres 官方 Yum 存储库

尽管 Linux yum 存储库中提供了 Postgres 数据库服务器,但是要安装最新版本,我们必须在我们的 Linux 服务器中添加 PostgreSQL 官方 yum 存储库。

# dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

通过执行以下命令禁用标准 yum 存储库中的 PostgreSQL 模块。

# dnf -qy module disable postgresql

什么是 pgAdmin?

pgAdmin 是用于 Postgres 数据库服务器的免费开源图形用户界面 (GUI) 管理工具。

pgAdmin 可用于桌面和 Web 界面。
pgAdmin 是 PostgreSQL 的其他管理工具中功能丰富且最受欢迎的。

就像 PostgreSQL 一样,pgAdmin 也是一个免费软件。
它也是在 PostgreSQL 许可下分发的。

什么是PostgreSQL?

PostgreSQL(也称为 Postgres)是一个免费的开源关系数据库管理系统 (RDBMS),强调可扩展性和 SQL 合规性。

PostgreSQL 最初被命名为 POSTGRES,指的是它起源于 Ingres 数据库的继承者。
1996 年,该项目更名为 PostgreSQL,以反映其对 SQL 的支持。

PostgreSQL 的事务具有原子性、一致性、隔离性、持久性 (ACID) 属性、可自动更新的视图、物化视图、触发器、外键和存储过程。
它旨在处理一系列工作负载,从单台机器到具有许多并发用户的数据仓库或者 Web 服务。
它是 macOS 服务器的默认数据库,也可用于 Linux、FreeBSD、OpenBSD 和 Windows。

PostgreSQL 是一个免费软件,在 PostgreSQL 许可下分发(免费和开源,许可)。

在 CentOS/RHEL 8 上安装 PostgreSQL 和 pgAdmin

PostgreSQL 是最先进的 RDBMS,而 pgAdmin 是 Postgres 的 Web UI。
在这里,我们将看到如何在 CentOS/RHEL 8 上 安装 PostgreSQL 和pgAdmin 。

在 CentOS 8 上安装 pgAdmin Web UI

已添加所有存储库。
我们现在可以使用 dnf 命令安装 pgAdmin。

# dnf install -y pgadmin4

要配置 SELinux 策略,pgAdmin 设置脚本需要 semanage 命令,该命令在 policycoreutils-python-utils 包中提供。
因此,我们应该在执行 pgAdmin 安装脚本之前安装它。

# dnf install -y policycoreutils-python-utils

pgAdmin 软件带有一个编写良好的配置脚本来配置 pgAdmin Web 服务。
执行它以创建管理员用户,配置 SELinux 策略和 Apache Web 服务器以部署 pgAdmin Web 服务。

# /usr/pgadmin4/bin/setup-web.sh
Setting up pgAdmin 4 in web mode on a Redhat platform...
Creating configuration database...
NOTE: Configuring authentication for SERVER mode.
Enter the email address and password to use for the initial pgAdmin user account:
Email address: jackli@postgres-01.onitroad.com
Password:
Retype password:
pgAdmin 4 - Application Initialisation
======================================
Creating storage and log directories...
Configuring SELinux...
The Apache web server is not running. We can enable and start the web server for you to finish pgAdmin 4 installation. Continue (y/n)? y
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service -> /usr/lib/systemd/system/httpd.service.
Apache successfully enabled.
Apache successfully started.
You can now start using pgAdmin 4 in web mode at http://127.0.0.1/pgadmin4

上面的脚本安装了一个配置文件 (/etc/httpd/conf.d/pgadmin4.conf ) 来在 Apache Web 服务器上部署 pgAdmin。

此文件中不需要自定义。
我们只需要启用和启动 httpd 服务。

# systemctl enable --now httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service -> /usr/lib/systemd/system/httpd.service.

配置 Linux 防火墙以允许传入流量到 Apache Web 服务器。

# firewall-cmd --permanent --add-service=http
success
# firewall-cmd --reload
success

在 Web 浏览器中打开 URL http://postgres-01.onitroad.com/pgadmin4/。

以我们通过 setup-web.sh 脚本创建的管理员用户身份登录 pgAdmin。

成功登录后,我们可以访问 pgAdmin 仪表板。

要在 pgAdmin 列表中添加 PostgreSQL 数据库服务器,请单击“添加新服务器”。

提供服务器名称并单击“连接”选项卡。

正如我们在上面的屏幕截图中所做的那样,在此对话框中提供数据库连接信息。

点击“保存”。

我们的 Postgres 数据库服务器已添加到 pgAdmin 中。
我们可以在左侧面板中看到数据库服务器的树。

为 Postgres 数据库服务配置 Linux 防火墙

在 CentOS 8 中,我们已经为 PostgreSQL 提供了防火墙服务,因此我们通过使用 firewall-cmd 命令来允许它。
但是,如果 PostgreSQL 服务器配置为侦听非默认端口,我们可以创建自己的自定义防火墙服务。

# firewall-cmd --permanent --add-service=postgresql
success
# firewall-cmd --reload
success

Postgres 数据库服务器已安装在 Linux 服务器上。

在 CentOS 8 上安装 Postgres 数据库服务器

使用 dnf 命令在 Linux 服务器上安装 Postgres 数据库服务器 13(在撰写本文时是最新的)。

# dnf install -y postgresql13-server

我们需要执行一次以下命令来初始化 Postgres 数据库。

# /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK

启用并启动 Postgres 数据库服务。

# systemctl enable --now postgresql-13
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-13.service -> /usr/lib/systemd/system/postgresql-13.service.

检查 Postgres 数据库服务的状态。

# systemctl status postgresql-13.service

检查已安装的 PostgreSQL 服务器的版本。

# psql -V
psql (PostgreSQL) 13.1

切换到 postgres 用户并连接到 psql shell 以设置管理员用户密码。

# su - postgres
$ psql
psql (13.1)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD '123';
ALTER ROLE
postgres=# \q
$ exit
logout
之路教程 https://onitr oad .com

为网络访问配置 Postgres 数据库服务

PostgreSQL 默认服务端口是 5432/tcp 。
执行以下命令来验证 PostgreSQL 服务是否正在侦听此端口。

# ss -tulpn | grep 5432
tcp     LISTEN   0        128            127.0.0.1:5432          0.0.0.0:*       users:(("postmaster",pid=1603,fd=7))
tcp     LISTEN   0        128                [::1]:5432             [::]:*       users:(("postmaster",pid=1603,fd=6))

我们可能会注意到 PostgreSQL 服务最初仅在 localhost 接口上运行。
但是,要使我们的 Postgres 数据库可用于网络客户端,我们必须将 PostgreSQL 配置为在所有(或者某些特定)接口上运行。

为此,我们必须在 vim 文本编辑器中编辑 PostgreSQL 配置文件。

# vi /var/lib/pgsql/13/data/postgresql.conf

其中搜索以下指令。

# listen_addresses = 'localhost'

并将其替换为以下指令。

listen_addresses = '*'

Postgres 数据库服务现已配置为侦听所有网络接口。

允许网络客户端访问 pg_hba.conf 文件中的 PostgreSQL 服务。

# echo "host all all 192.168.1.0/24 md5" >> /var/lib/pgsql/13/data/pg_hba.conf

重新启动 Postgres 数据库服务使更改生效。

# systemctl restart postgresql-13.service

再次使用 ss 命令检查网络服务。

# ss -tulpn | grep 5432
tcp     LISTEN   0        128              0.0.0.0:5432          0.0.0.0:*       users:(("postmaster",pid=1781,fd=6))
tcp     LISTEN   0        128                 [::]:5432             [::]:*       users:(("postmaster",pid=1781,fd=7))

Postgres 服务现在在所有网络接口上运行。

日期:2020-09-17 00:11:18 来源:oir作者:oir