在 CentOS 7 上安装 PostgreSQL 11
以 root 用户身份使用 ssh 连接 postgresql-01.onitroad.com。
在 CentOS 7.6 中,PostgreSQL 9.2 可在标准 yum 存储库中使用。
但是我们需要安装最新版本的 PostgreSQL 例如:11.
因此,我们必须在 CentOS 7 服务器中添加 PostgreSQL yum 存储库,然后才能安装 PostgreSQL 11.
可以从 PostgreSQL 官方下载页面获得各种 Linux 发行版的 Yum 存储库。
使用以下命令安装 PostgreSQL yum 存储库。
[root@postgresql-01 ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
禁用旧版本的 PostgreSQL yum 存储库。
[root@postgresql-01 ~]# yum-config-manager --disable pgdg10 pgdg94 pgdg95 pgdg96
为 yum 存储库构建缓存。
[root@postgresql-01 ~]# yum makecache fast
使用 yum 命令安装 PostgreSQL 11 客户端和服务器包。
[root@postgresql-01 ~]# yum install -y postgresql11 postgresql11-server
初始化 PostgreSQL 数据库实例如下。
[root@postgresql-01 ~]# /usr/pgsql-11/bin/postgresql-11-setup initdb Initializing database ... OK
启用并启动 PostgreSQL 服务。
[root@postgresql-01 ~]# systemctl enable postgresql-11.service Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-11.service to /usr/lib/systemd/system/postgresql-11.service. [root@postgresql-01 ~]# systemctl start postgresql-11.service
以 postgres 用户身份连接并设置管理员密码。
[root@postgresql-01 ~]# su - postgres -bash-4.2$ psql psql (11.4) Type "help" for help. postgres=# ALTER USER postgres WITH PASSWORD '123'; ALTER ROLE postgres=# \q -bash-4.2$ exit logout
PostgreSQL 11 服务器已安装在我们的 CentOS 7 服务器上。
在 CentOS 7 上配置 PostgreSQL 远程访问
默认情况下,PostgreSQL 服务在端口 5432/tcp 上本地运行。
但是,如果需要,我们可以将其配置为从我们网络中的其他计算机进行远程访问。
编辑 PostgreSQL 配置文件。
[root@postgresql-01 ~]# vi /var/lib/pgsql/11/data/postgresql.conf
在此文件中查找并设置以下指令。
listen_addresses = '*'
允许网络客户端访问 pg_hba.conf 文件中的 PostgreSQL 服务。
[root@postgresql-01 ~]# echo "host all all 192.168.1.0/24 md5" >> /var/lib/pgsql/11/data/pg_hba.conf
重新启动 PostgreSQL 服务使更改生效。
[root@postgresql-01 ~]# systemctl restart postgresql-11.service
在 Linux 防火墙中允许 PostgreSQL 服务。
[root@postgresql-01 ~]# firewall-cmd --permanent --add-service=postgresql success [root@postgresql-01 ~]# firewall-cmd --reload success
我们的 PostgreSQL 服务配置为远程访问。
PostgreSQL(或者 Postgres)是一个免费的开源关系数据库管理系统 (RDBMS),强调可扩展性和技术标准合规性。
pgAdmin 是最流行的开源和功能丰富的 Web 界面,用于管理 PostgreSQL 数据库服务器。
在本文中,我们将在 CentOS 7 服务器上安装 PostgreSQL 11 和 pgAdmin 4.
在 CentOS 7 上安装 pgAdmin 4 Web 界面
为了简化 PostgreSQL 的数据库管理过程,我们在 CentOS 7 服务器上安装了一个流行的 SQL Web 界面,例如:pgAdmin 4.
pgAdmin 4 在同一个 PostgreSQL 11 yum 存储库中可用。
但首先我们要安装 EPEL (Extra Packages for Enterprise Linux) yum 存储库,因为 pgAdmin 4 需要一些在 EPEL yum 存储库中可用的包。
[root@postgresql-01 ~]# yum install -y epel-release
为 EPEL 存储库构建 yum 缓存。
[root@postgresql-01 ~]# yum makecache fast
使用 yum 命令安装 pgAdmin 4.
[root@postgresql-01 ~]# yum install -y pgadmin4
pgAdmin 4 是一个基于 python 的 web 应用程序,因此它需要一个支持 python 语言的 web 服务器来部署。
幸运的是,pgAdmin 4 在安装过程中会自动安装 Apache HTTP 服务器和 Python 语言支持。
启用并启动 Apache 服务。
[root@postgresql-01 ~]# systemctl enable httpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@postgresql-01 ~]# systemctl start httpd.service
在 Linux 防火墙中允许 HTTP 服务。
[root@postgresql-01 ~]# firewall-cmd --permanent --add-service=http success [root@postgresql-01 ~]# firewall-cmd --reload success
pgAdmin 4 还在 Apache 配置目录中安装了一个配置文件。
因此,要启用 pgAdmin Web 应用程序,我们必须重命名此文件以使 httpd.service 可读。
[root@postgresql-01 ~]# mv /etc/httpd/conf.d/pgadmin4.conf.sample /etc/httpd/conf.d/pgadmin4.conf
编辑/etc/httpd/conf.d/pgadmin4.conf文件,编辑后的文件内容如下。
(黄色线已在此处添加)。
<VirtualHost *:80> ServerName 192.168.1.184 LoadModule wsgi_module modules/mod_wsgi.so WSGIDaemonProcess pgadmin processes=1 threads=25 WSGIScriptAlias /pgadmin4 /usr/lib/python2.7/site-packages/pgadmin4-web/pgAdmin4.wsgi <Directory /usr/lib/python2.7/site-packages/pgadmin4-web/> WSGIProcessGroup pgadmin WSGIApplicationGroup %{GLOBAL} <IfModule mod_authz_core.c> # Apache 2.4 Require all granted </IfModule> <IfModule !mod_authz_core.c> # Apache 2.2 Order Deny,Allow Deny from All Allow from 127.0.0.1 Allow from ::1 </IfModule> </Directory> </VirtualHost>
在以下文件中定义路径变量。
[root@postgresql-01 ~]# vi /usr/lib/python2.7/site-packages/pgadmin4-web/config_distro.py
其中添加以下设置。
SQLITE_PATH = '/var/lib/pgadmin/pgadmin.db' SESSION_DB_PATH = '/var/lib/pgadmin/sessions' STORAGE_DIR = '/var/lib/pgadmin/storage' LOG_FILE = '/var/log/pgadmin/pgadmin.log'
调整文件权限。
[root@postgresql-01 ~]# chown -R apache:apache /var/log/pgadmin/ [root@postgresql-01 ~]# chown -R apache:apache /var/lib/pgadmin/
设置 SELinux 文件上下文。
[root@postgresql-01 ~]# semanage fcontext -a -t httpd_sys_rw_content_t "/var/lib/pgadmin(/.*)?" [root@postgresql-01 ~]# restorecon -R /var/lib/pgadmin/ [root@postgresql-01 ~]# semanage fcontext -a -t httpd_sys_rw_content_t "/var/log/pgadmin(/.*)?" [root@postgresql-01 ~]# restorecon -R /var/log/pgadmin/
设置 SELinux boolean,以便 Apache 可以访问 PostgreSQL 服务端口。
[root@postgresql-01 ~]# setsebool -P httpd_can_network_connect_db 1
重新启动 Apache 服务使更改生效。
[root@postgresql-01 ~]# systemctl restart httpd.service
初始化 pgAdmin 4 应用程序如下。
[root@postgresql-01 ~]# python /usr/lib/python2.7/site-packages/pgadmin4-web/setup.py NOTE: Configuring authentication for SERVER mode. Enter the email address and password to use for the initial pgAdmin user account: Email address: jackli@postgresql-01.onitroad.com Password: Retype password: pgAdmin 4 - Application Initialisation ======================================
在客户端浏览器中浏览 URL http://postgresql-01.onitroad.com/pgadmin4/。
使用我们在上一步中定义的电子邮件和密码登录。
单击添加新服务器将我们的 PostgreSQL 数据库服务器添加到 pgAdmin 4 Web 界面。
根据上面的屏幕截图提供必要的信息。
单击连接选项卡并按照以下屏幕截图提供连接信息。
单击保存。
我们的 PostgreSQL 数据库已添加到 pgAdmin 4 SQL Web 界面中。
我们已经在 CentOS 7 服务器上成功安装了 PostgreSQL 11 和 pgAdmin 4.