「PostgreSQL 」如何在CentOS 7 / CentOS 8上安装PostgreSQL 12

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介: 「PostgreSQL 」如何在CentOS 7 / CentOS 8上安装PostgreSQL 12


本指南将引导您完成在CentOS 7 / CentOS 8 Linux服务器上安装PostgreSQL 12的步骤。PostgreSQL是一个基于POSTGRES 4.2的对象关系数据库管理系统。开发人员和数据库管理员可以使用PostgreSQL 12。PostgreSQL项目为最常见的发行版提供了所有受支持版本的软件包的存储库。支持的发行版包括所有Red Hat系列,其中包括CentOS,Fedora,Scientific Linux,Oracle Linux和Red Hat Enterprise Linux。

使用以下步骤在CentOS 8 / CentOS 7上安装PostgreSQL 12。

步骤1:将PostgreSQL Yum存储库添加到CentOS 7 / CentOS 8

PostgreSQL Yum存储库将与您的常规系统和补丁程序管理集成,并在PostgreSQL的整个支持期限内为所有受支持的PostgreSQL版本提供自动更新。

可以通过运行以下命令将其添加到CentOS系统中:

CentOS 8:

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

CentOS 7:

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

您可以通过运行以下命令获取有关已安装软件包的更多信息:

$ rpm -qi pgdg-redhat-repo

Name : pgdg-redhat-repo

Version : 42.0

Release : 4

Architecture: noarch

Install Date: Thu 19 Sep 2019 06:34:53 PM UTC

Group : System Environment/Base

Size : 6915

License : PostgreSQL

Signature : DSA/SHA1, Wed 17 Apr 2019 04:12:42 AM UTC, Key ID 1f16d2e1442df0f8

Source RPM : pgdg-redhat-repo-42.0-4.src.rpm

Build Date : Wed 17 Apr 2019 04:12:41 AM UTC

Build Host : koji-centos7-x86-64-pgbuild

Relocations : (not relocatable)

Vendor : PostgreSQL Global Development Group

URL : https://yum.postgresql.org

Summary : PostgreSQL PGDG RPMs- Yum Repository Configuration for Red Hat / CentOS / Scientific Linux

Description :

This package contains yum configuration for Red Hat Enterprise Linux, CentOS

and Scientific Linux. and also the GPG key for PGDG RPMs.

步骤2:在CentOS 8 / CentOS 7上安装PostgreSQL 12

添加YUM存储库后,我们可以使用以下命令在CentOS 7/8上安装PostgreSQL 12。

CentOS 8上的PostgreSQL 12

禁用内置的PostgreSQL模块:

sudo dnf -qy module disable postgresql

然后安装客户端和服务器软件包:

sudo dnf -y install postgresql12 postgresql12-server

CentOS 7上的PostgreSQL 12

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

sudo yum -y install epel-release yum-utils sudo yum-config-manager --enable pgdg12 sudo yum install postgresql12-server postgresql12

样本安装输出:

Dependencies Resolved

===================================================================================================================================================

Package Arch Version Repository Size

===================================================================================================================================================

Installing:

postgresql12 x86_64 12beta4-1PGDG.rhel7 pgdg12-testing 1.8 M

postgresql12-server x86_64 12beta4-1PGDG.rhel7 pgdg12-testing 5.4 M

Installing for dependencies:

libicu x86_64 50.2-3.el7 base 6.9 M

postgresql12-libs x86_64 12beta4-1PGDG.rhel7 pgdg12-testing 383 k

python3 x86_64 3.6.8-10.el7 base 69 k

python3-libs x86_64 3.6.8-10.el7 base 7.0 M

python3-pip noarch 9.0.3-5.el7 base 1.8 M

python3-setuptools noarch 39.2.0-10.el7 base 629 k

Transaction Summary

===================================================================================================================================================

Install 2 Packages (+6 Dependent packages)

Total download size: 24 M

Installed size: 104 M

Downloading packages:

(1/8): libicu-50.2-3.el7.x86_64.rpm | 6.9 MB 00:00:00

warning: /var/cache/yum/x86_64/7/pgdg12-testing/packages/postgresql12-libs-12beta4-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY

Public key for postgresql12-libs-12beta4-1PGDG.rhel7.x86_64.rpm is not installed

(2/8): postgresql12-libs-12beta4-1PGDG.rhel7.x86_64.rpm | 383 kB 00:00:00

(3/8): python3-3.6.8-10.el7.x86_64.rpm | 69 kB 00:00:00

(4/8): python3-setuptools-39.2.0-10.el7.noarch.rpm | 629 kB 00:00:00

(5/8): postgresql12-12beta4-1PGDG.rhel7.x86_64.rpm | 1.8 MB 00:00:00

(6/8): python3-libs-3.6.8-10.el7.x86_64.rpm | 7.0 MB 00:00:00

(7/8): postgresql12-server-12beta4-1PGDG.rhel7.x86_64.rpm | 5.4 MB 00:00:00

(8/8): python3-pip-9.0.3-5.el7.noarch.rpm | 1.8 MB 00:00:00

---------------------------------------------------------------------------------------------------------------------------------------------------

Total 15 MB/s | 24 MB 00:00:01

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG

Importing GPG key 0x442DF0F8:

Userid : "PostgreSQL RPM Building Project <pgsqlrpms-hackers@pgfoundry.org>"

Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8

Package : pgdg-redhat-repo-42.0-4.noarch (installed)

From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : libicu-50.2-3.el7.x86_64 1/8

Installing : postgresql12-libs-12beta4-1PGDG.rhel7.x86_64 2/8

Installing : python3-libs-3.6.8-10.el7.x86_64 3/8

Installing : python3-setuptools-39.2.0-10.el7.noarch 4/8

Installing : python3-3.6.8-10.el7.x86_64 5/8

Installing : python3-pip-9.0.3-5.el7.noarch 6/8

Installing : postgresql12-12beta4-1PGDG.rhel7.x86_64 7/8

Installing : postgresql12-server-12beta4-1PGDG.rhel7.x86_64 8/8

Verifying : postgresql12-libs-12beta4-1PGDG.rhel7.x86_64 1/8

Verifying : python3-pip-9.0.3-5.el7.noarch 2/8

Verifying : libicu-50.2-3.el7.x86_64 3/8

Verifying : python3-libs-3.6.8-10.el7.x86_64 4/8

Verifying : postgresql12-12beta4-1PGDG.rhel7.x86_64 5/8

Verifying : postgresql12-server-12beta4-1PGDG.rhel7.x86_64 6/8

Verifying : python3-setuptools-39.2.0-10.el7.noarch 7/8

Verifying : python3-3.6.8-10.el7.x86_64 8/8

Installed:

postgresql12.x86_64 0:12beta4-1PGDG.rhel7 postgresql12-server.x86_64 0:12beta4-1PGDG.rhel7

Dependency Installed:

libicu.x86_64 0:50.2-3.el7 postgresql12-libs.x86_64 0:12beta4-1PGDG.rhel7 python3.x86_64 0:3.6.8-10.el7 python3-libs.x86_64 0:3.6.8-10.el7

python3-pip.noarch 0:9.0.3-5.el7 python3-setuptools.noarch 0:39.2.0-10.el7


步骤3:初始化并启动数据库服务

安装后,需要先进行数据库初始化,然后才能启动服务。

sudo /usr/pgsql-12/bin/postgresql-12-setup initdb

数据库主要配置ifile写入:/var/lib/pgsql/12/data/postgresql.conf

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

sudo systemctl enable --now postgresql-12

确认服务已启动,没有任何错误。

$ systemctl status postgresql-12

● postgresql-12.service - PostgreSQL 12 database server

Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)

Active: active (running) since Thu 2019-09-19 18:50:10 UTC; 39s ago

Docs: https://www.postgresql.org/docs/12/static/

Process: 10647 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)

Main PID: 10652 (postmaster)

CGroup: /system.slice/postgresql-12.service

├─10652 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/

├─10654 postgres: logger

├─10656 postgres: checkpointer

├─10657 postgres: background writer

├─10658 postgres: walwriter

├─10659 postgres: autovacuum launcher

├─10660 postgres: stats collector

└─10661 postgres: logical replication launcher

Sep 19 18:50:10 cent7.novalocal systemd[1]: Starting PostgreSQL 12 database server...

Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.207 UTC [10652] LOG: starting PostgreSQL 12beta4 on x86_64-pc-lin... 64-bit

Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.209 UTC [10652] LOG: listening on IPv6 address "::1", port 5432

Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.209 UTC [10652] LOG: listening on IPv4 address "127.0.0.1", port 5432

Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.214 UTC [10652] LOG: listening on Unix socket "/var/run/postgresq...L.5432"

Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.229 UTC [10652] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"

Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.254 UTC [10652] LOG: redirecting log output to logging collector process

Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.254 UTC [10652] HINT: Future log output will appear in directory "log".

Sep 19 18:50:10 cent7.novalocal systemd[1]: Started PostgreSQL 12 database server.

Hint: Some lines were ellipsized, use -l to show in full.

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

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

第4步:设置PostgreSQL管理员用户的密码

设置PostgreSQL管理员用户

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

步骤5:启用远程访问(可选)

编辑文件/var/lib/pgsql/12/data/postgresql.conf并将所有服务器的“监听地址”设置为服务器IP地址或“ *”。

listen_addresses ='192.168.10.10'

还设置PostgreSQL接受远程连接

$ sudo vim /var/lib/pgsql/12/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-12

步骤6:安装pgAdmin 4 Web界面

pgAdmin是领先的开源功能丰富的PostgreSQL管理和开发平台,可在Linux,Unix,Mac OS X和Windows上运行。这是在CentOS上安装pgAdmin4的链接。

  • 如何在CentOS 8 Linux上安装pgAdmin 4
  • 在CentOS 7上安装pgAdmin4
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
4天前
|
安全 关系型数据库 MySQL
CentOS 8 中安装与配置 MySQL
CentOS 8 中安装与配置 MySQL
28 3
|
6天前
|
消息中间件 Linux
Centos安装RabbitMQ
Centos安装RabbitMQ
15 3
|
6天前
|
Linux Docker 容器
Centos8安装Docker
Centos8安装Docker
33 1
|
6天前
|
Linux 测试技术 开发工具
CentOS Linux 8使用阿里源(安装jdk11、git测试)
CentOS Linux 8使用阿里源(安装jdk11、git测试)
24 1
|
7天前
|
存储 Linux 数据安全/隐私保护
Centos安装
Centos安装
29 2
|
4天前
|
SQL 关系型数据库 分布式数据库
PolarDB操作报错合集之安装时出现报错,一般是什么导致的
在使用阿里云的PolarDB(包括PolarDB-X)时,用户可能会遇到各种操作报错。下面汇总了一些常见的报错情况及其可能的原因和解决办法:1.安装PolarDB-X报错、2.PolarDB安装后无法连接、3.PolarDB-X 使用rpm安装启动卡顿、4.PolarDB执行UPDATE/INSERT报错、5.DDL操作提示“Lock conflict”、6.数据集成时联通PolarDB报错、7.编译DN报错(RockyLinux)、8.CheckStorage报错(源数据库实例被删除)、9.嵌套事务错误(TDDL-4604)。
|
5天前
|
缓存 Linux Docker
CentOS 7 下安装 Docker 及配置阿里云加速服务
CentOS 7 下安装 Docker 及配置阿里云加速服务
102 0
|
6天前
|
Java Linux
Centos安装openjdk11并配置JAVA_HOME
Centos安装openjdk11并配置JAVA_HOME
13 0
|
6天前
|
Linux
centos如何安装libssl-dev libsdl-dev libavcodec-dev libavutil-dev ffmpeg
centos如何安装libssl-dev libsdl-dev libavcodec-dev libavutil-dev ffmpeg
11 0
|
7天前
|
Linux 网络安全 数据安全/隐私保护
centos安装snmp并创建V3账号
centos安装snmp并创建V3账号
8 0