如何在CentOS 7上安装PostgreSQL 11

时间:2020-02-23 14:30:51  来源:igfitidea点击:

本指南将在CentOS 7上安装PostgreSQL11. PostgreSQL是世界上最先进,功能最强大的开源关系数据库系统,拥有30多年的积极开发经验,在可靠性,功能强大和性能方面赢得了极高的声誉。

PostgreSQL 11于2016-10-18发行,我们可以从Release页面检查其新功能。通过以下步骤从PostgreSQL Yum存储库中在CentOS 7上安装PostgreSQL 11:

更新系统

确保系统软件包是最新的:

sudo yum update -y

由于我们可能具有内核更新,因此建议我们在升级后重新引导系统

sudo reboot

添加PostgreSQL Yum存储库

通过运行以下命令将PostgreSQL Yum Repository添加到CentOS 7系统

sudo yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm

当提示我们确认安装时,请按它们

.......................
Dependencies Resolved

======================================================================================================================================================
 Package                             Arch                         Version                      Repository                                        Size
======================================================================================================================================================
Installing:
 pgdg-centos11                       noarch                       11-2                         /pgdg-centos11-11-2.noarch                       2.7 k

Transaction Summary
======================================================================================================================================================
Install  1 Package

Total size: 2.7 k
Installed size: 2.7 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : pgdg-centos11-11-2.noarch                                                                                                          1/1 
  Verifying  : pgdg-centos11-11-2.noarch                                                                                                          1/1 

Installed:
  pgdg-centos11.noarch 0:11-2                                                                                                                         

Complete!

安装PostgreSQL服务器和客户端软件包

添加PostgreSQL Yum存储库后,安装PostgreSQL服务器/客户端软件包:

sudo yum -y install postgresql11-server postgresql11

确认已安装的软件包:

$sudo rpm -qi postgresql11-server  
Name        : postgresql11-server
Version     : 11.0
Release     : 1PGDG.rhel7
Architecture: x86_64
Install Date: Sat 03 Nov 2016 04:43:38 PM UTC
Group       : Applications/Databases
Size        : 19382677
License     : PostgreSQL
Signature   : DSA/SHA1, Thu 18 Oct 2016 09:06:24 AM UTC, Key ID 1f16d2e1442df0f8
Source RPM  : postgresql11-11.0-1PGDG.rhel7.src.rpm
Build Date  : Tue 16 Oct 2016 02:28:25 AM UTC
Build Host  : koji-centos7-x86-64-pgbuild
Relocations : (not relocatable)
Vendor      : PostgreSQL Global Development Group
URL         : https://www.postgresql.org/
Summary     : The programs needed to create and run a PostgreSQL server
Description :
PostgreSQL is an advanced Object-Relational database management system (DBMS).
The postgresql11-server package contains the programs needed to create
and run a PostgreSQL server, which will in turn allow you to create
and maintain PostgreSQL databases.

初始化数据库并启用自动启动

现在已经安装了数据库软件包,通过运行以下命令来初始化数据库

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

然后启动并启用服务以在启动时启动

sudo systemctl start postgresql-11
sudo systemctl enable postgresql-11

PostgreSQL 11的配置文件是/var/lib/pgsql/11/data/postgresql.conf

如果我们有正在运行的防火墙服务,并且远程客户端应连接到数据库服务器,请允许PostgreSQLservice。

sudo firewall-cmd --add-service=postgresql --permanent
sudo firewall-cmd --reload

对PostgreSQL启用远程访问

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

listen_addresses = '192.168.18.9'

还设置PostgreSQL接受远程连接

$sudo vim /var/lib/pgsql/11/data/pg_hba.conf

# Accept from anywhere
host all all 0.0.0.0/0 md5

# Accept from trusted subnet
host all all 192.168.18.0/24 md5

重新启动服务

sudo systemctl restart postgresql-11

设置PostgreSQL管理员用户密码

设置PostgreSQL管理员用户

$sudo su - postgres 
bash-4.2$psql -c "alter user postgres with password 'StrongPassword'" 
ALTER ROLE
-bash-4.2$

创建测试用户和数据库

-bash-4.2$createuser test_user
-bash-4.2$createdb test_db -O test_user
-bash-4.2$grant all privileges on database test_db to test_user;

以" test_user"用户身份登录,尝试在数据库上创建表。

$psql -U test_user -h localhost -d test_db

在CentOS 7/Fedora 29/Fedora 28上安装pgAdmin 4

在CentOS 7/Fedora 29/Fedora 28上安装pgAdmin 4,以通过Web界面管理PostgreSQL。