如何在CentOS 7上安装PostgreSQL 13

时间:2020-02-23 14:32:12  来源:igfitidea点击:

PostgreSQL是基于POSTGRES 4.2的对象关系数据库管理系统。 PostgreSQL 13已发布,供公众和生产使用。 PostgreSQL在所有主要的Linux操作系统上运行,自2001年以来一直符合ACID。此外,它还具有强大的添加组件,例如流行的PostGISgeospatial数据库扩展器。本教程将引导我们完成在CentOS 7上PostgreSQL 13的安装。

PostgreSQL开发团队在在线YUM和APT存储库中分别为基于RHEL和Debian的系统提供了预先构建的软件包。支持的发行版包括所有Red Hat系列,其中包括CentOS,Fedora,Scientific Linux,Oracle Linux和Red Hat Enterprise Linux。我们现在可以在CentOS 7上开始安装PostgreSQL 13.

如何在CentOS 7上安装PostgreSQL 13

使用以下步骤在CentOS 7 Linux上安装PostgreSQL 13. 我们最近在CentOS/RHEL 8系统上创建了关于PostgreSQL 13安装的单独文章。

将PostgreSQL Yum存储库添加到CentOS 7

PostgreSQL Yum存储库将与常规系统和补丁程序管理集成,并在PostgreSQL的整个支持生命周期内为所有受支持的PostgreSQL版本提供自动更新。运行以下命令以将PostgreSQL 13存储库添加到CentOS 7 Linux计算机上。

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

我建议我们在添加存储库后执行系统重新引导。

sudo yum -y update 
sudo reboot

在CentOS 7上安装PostgreSQL 13

成功添加存储库后,我们可以继续在CentOS 7上安装PostgreSQL 13.

确认启用的存储库列表。

$sudo yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.alpha-labs.net
 * extras: artfiles.org
 * updates: mirror.plustech.de
repo id               repo name                                           status
base/7/x86_64         CentOS-7 - Base                                     10,070
extras/7/x86_64       CentOS-7 - Extras                                      413
pgdg-common/7/x86_64  PostgreSQL common RPMs for RHEL/CentOS 7 - x86_64      360
pgdg10/7/x86_64       PostgreSQL 10 for RHEL/CentOS 7 - x86_64               789
pgdg11/7/x86_64       PostgreSQL 11 for RHEL/CentOS 7 - x86_64               838
pgdg12/7/x86_64       PostgreSQL 12 for RHEL/CentOS 7 - x86_64               414
pgdg13/7/x86_64       PostgreSQL 13 for RHEL/CentOS 7 - x86_64               140
pgdg95/7/x86_64       PostgreSQL 9.5 for RHEL/CentOS 7 - x86_64              698
pgdg96/7/x86_64       PostgreSQL 9.6 for RHEL/CentOS 7 - x86_64              759
updates/7/x86_64      CentOS-7 - Updates                                   1,134
repolist: 15,615

检查存储库中是否有PostgreSQL 13软件包。

$sudo yum search postgresql13
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.alpha-labs.net
 * extras: artfiles.org
 * updates: mirror.plustech.de
========================== N/S matched: postgresql13 ===========================
postgresql13.x86_64 : PostgreSQL client programs and libraries
postgresql13-contrib.x86_64 : Contributed source and binaries distributed with
                            : PostgreSQL
postgresql13-devel.x86_64 : PostgreSQL development header files and libraries
postgresql13-docs.x86_64 : Extra documentation for PostgreSQL
postgresql13-libs.x86_64 : The shared libraries required for any PostgreSQL
                         : clients
postgresql13-llvmjit.x86_64 : Just-in-time compilation support for PostgreSQL
postgresql13-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql13-plpython3.x86_64 : The Python3 procedural language for PostgreSQL
postgresql13-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql13-server.x86_64 : The programs needed to create and run a PostgreSQL
                           : server
postgresql13-test.x86_64 : The test suite distributed with PostgreSQL

Name and summary matches only, use "search all" for everything.

现在在CentOS 7上安装PostgreSQL 13软件包。

sudo yum -y install postgresql13 postgresql13-server

当安装程序询问时,导入GPG密钥。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                             7.0 MB/s |  15 MB  00:00:02
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <theitroad@localhost>"
 Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
 Package    : pgdg-redhat-repo-42.0-11.noarch (@/pgdg-redhat-repo-latest.noarch)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Is this ok [y/N]: y

初始化并启动数据库服务

在CentOS 7上全新安装PostgreSQL 13之后,需要初始化。

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

启动PostgreSQL数据库服务。

sudo systemctl start postgresql-13

检查服务状态以确认它正在运行。

$systemctl status postgresql-13
● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2017-07-09 23:35:30 CEST; 37s ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 1860 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 1865 (postmaster)
    Tasks: 8 (limit: 12210)
   Memory: 17.3M
   CGroup: /system.slice/postgresql-13.service
           ├─1865 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
           ├─1867 postgres: logger
           ├─1869 postgres: checkpointer
           ├─1870 postgres: background writer
           ├─1871 postgres: walwriter
           ├─1872 postgres: autovacuum launcher
           ├─1873 postgres: stats collector
           └─1874 postgres: logical replication launcher

May 09 23:35:30 centos-01.theitroad.local systemd[1]: Starting PostgreSQL 13 database server...
May 09 23:35:30 centos-01.theitroad.local postmaster[1865]: 2017-07-09 23:35:30.180 CEST [1865] LOG:  redirecting log output to logging collector process
May 09 23:35:30 centos-01.theitroad.local postmaster[1865]: 2017-07-09 23:35:30.180 CEST [1865] HINT:  Future log output will appear in directory "log".
May 09 23:35:30 centos-01.theitroad.local systemd[1]: Started PostgreSQL 13 database server.

重新启动系统后,启用服务以启动。

$sudo systemctl enable postgresql-13
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-13.service to /usr/lib/systemd/system/postgresql-13.service.

设置PostgreSQL管理员用户密码

更新PostgreSQL管理员用户密码。

$sudo su - postgres 
]$psql -c "alter user postgres with password 'StrongDBPassword'"
ALTER ROLE

启用远程数据库连接(可选)

编辑文件/var/lib/pgsql/13/data/postgresql.conf,并将侦听地址设置为服务器IP地址,或者将*设置为所有接口。

$sudo vi /var/lib/pgsql/13/data/postgresql.conf
# line 59
listen_addresses = '172.20.11.11'

还设置PostgreSQL接受远程连接

$sudo vi /var/lib/pgsql/13/data/pg_hba.conf

# Accept from anywhere (not recommended)
host all all 0.0.0.0/0 md5

# Accept from trusted subnet (Recommended setting)
host all all 172.20.11.0/24 md5

保存更改后,重新启动数据库服务。

sudo systemctl restart postgresql-13

使用psql命令测试连接,同时提供用户名和(可选)数据库名称。

$psql -U <dbuser> -h <serverip> -p 5432 <dbname>

安装pgAdmin 4 Web界面

pgAdmin是一个用于管理PostgreSQL数据库服务器的开源工具。使用下面的教程在CentOS 7计算机上安装和使用pgAdmin4.
如何在CentOS 7上安装pgAdmin 4