PostgreSQL 安装部署

Posted P10ZHUO

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL 安装部署相关的知识,希望对你有一定的参考价值。

安装概述

编写目的

编写此文档为了更好的指导Oracle补丁安装工作,细化工作任务,规范安装升级操作。

背景

Oracle对于其产品每个季度发行一次的安全补丁包CPU(Critical Patch Update)与PSU(Patch Set Update)补丁,通常是为了修复产品中的安全隐患,并可能包含对一些严重bug以及功能组件的修复。
对于已知的安全漏洞及安全小组检测到的安全漏洞,本次通过安装数据库安全补丁的方式修复安全漏洞。

安装前准备

文件系统建议使用LVM管理。
不同的包下载地址不一样。

操作系统选择

推荐使用RHEL 7版本,本文采用RHEL 7.6
使用如下2个命令确认操作系统版本

[root@postgre ~]# uname -osr
Linux 3.10.0-957.el7.x86_64 GNU/Linux
[root@postgre ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.6 (Maipo)

系统基本检查和调整

检查以下项目,不符合的需要调整:

  1. 用uname -r 命令检查是否是x86_64 位系统
  2. 关闭SELinux:修改/etc/selinux/config 文件,将SELINUX=后面的值改为disabled。
    这一改动需要重启操作系统才生效。

安装依赖包

在依赖的系统包当中,readline 是命令行上下翻页的包,zlib 是备份压缩选项用的包。除
了下面列出的依赖包,其他按需安装。
⚫ GNU make version 3.80 or newer is required
⚫ GCC
⚫ tar
⚫ Readline
⚫ zlib
⚫ Perl 5.8.3 or later is needed

yum -y install readline readline-devel zlib zlib-devel gettext gettext-devel openssl openssl-devel pam pam-devel libxml2 libxml2-devel libxslt libxslt-devel 
perl perl-devel tcl-devel uuid-devel gcc gcc-c++ make flex bison perl-ExtUtils*

解释,后面编译会用到make和gmake2个编译工具:
gmake是GNU Make的缩写。 Linux系统环境下的make就是GNU Make,之所以有gmake,是因为在别的平台上,make一般被占用,GNU make只好叫gmake了。 比如在安装二进制文件进行编译时要使用make命令,但如果在Solaris或其他非GNU系统中运行,必须使用GNU make,而不是使用系统自带的make版本,这时要用gmake代替make进行编译。
gmake是指GNU make。在linux环境下gmake和make是一样的。

具体步骤如下:

[root@postgre yum.repos.d]# mount /dev/sr0 /mnt/            
[root@postgre ~]# cd /etc/yum.repos.d/
[root@postgre yum.repos.d]# ll
total 4
-rw-r--r--. 1 root root 358 Mar 30 18:18 redhat.repo
[root@postgre yum.repos.d]# rm -rf redhat.repo 
[root@postgre yum.repos.d]# cat base.repo 
[base]
name=base
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@postgre yum.repos.d]# yum clean all
[root@postgre yum.repos.d]# yum -y install readline readline-devel zlib zlib-devel gettext gettext-devel openssl openssl-devel pam pam-devel libxml2 libxml2-devel libxslt libxslt-devel perl perl-devel tcl-devel uuid-devel gcc gcc-c++ make flex bison perl-ExtUtils*

编辑hosts文件

将主机名和ip 解析写到/etc/hosts 文件。
如下所示:

[root@postgre ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.11.30      postgre

内核参数调整

echo “fs.file-max = 76724200
kernel.sem = 10000 10240000 10000 1024
kernel.shmmni = 4096
kernel.shmall = 253702
kernel.shmmax = 1039163392
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_max = 1048576
fs.aio-max-nr = 40960000
vm.dirty_ratio=20
vm.dirty_background_ratio=3
vm.dirty_writeback_centisecs=100
vm.dirty_expire_centisecs=500
vm.swappiness=10
vm.min_free_kbytes=524288”>>/etc/sysctl.conf && sysctl --system

时间和时间同步

查看时区
timedatectl status
确认时区,并且时间同步。建议主机工程师设置时间同步。
修改时区:
timedatectl set-timezone Asia/Shanghai

用户limits设置

echo “postgres soft nofile 1048576
postgres hard nofile 1048576
postgres soft nproc 131072
postgres hard nproc 131072
postgres soft stack 10240
postgres hard stack 32768
postgres soft core 6291456
postgres hard core 6291456” >>/etc/security/limits.conf

创建用户和组

groupadd dba -g 1001
useradd -g dba -u 1001 -d /home/postgres -s /bin/bash -c “PostgerSql Software Owner” postgres
echo “postgres” | passwd --stdin postgres

目录规划

mkdir /soft
chmod 777 /soft
mkdir -p /opt/pg12.7
chown -R postgres:dba /opt/pg12.7
mkdir -p /opt/pg12.7/pgdata/log
chmod 700 /opt/pg12.7/pgdata/log

sudo权限配置

#visudo -f /etc/sudoers
最后一行添加:

postgres ALL = NOPASSWD:/usr/bin/systemctl stop postgres-12.service,/usr/bin/systemctl start postgres-12.service,/usr/bin/systemctl restart postgres-12.service,/usr/bin/systemctl reload postgres-12.service,/usr/bin/systemctl status postgres-12.service

注意:一定保证是一行,不能跨行。

下载安装包

源码包下载

https://www.postgresql.org/ftp/source/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

RPM包下载

下载地址:https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/
在这里插入图片描述
选择具体对应的版本和os版本:
postgresql12-12.7-1PGDG.rhel7.x86_64.rpm
postgresql12-contrib-12.7-1PGDG.rhel7.x86_64.rpm
postgresql12-devel-12.7-1PGDG.rhel7.x86_64.rpm
postgresql12-libs-12.7-1PGDG.rhel7.x86_64.rpm
postgresql12-server-12.7-1PGDG.rhel7.x86_64.rpm
上面5个包必须的,解释如下:
postgresql12 - PostgreSQL client programs and libraries
postgresql12-contrib - Contributed source and binaries distributed with PostgreSQL
postgresql12-libs - The shared libraries required for any PostgreSQL clients
postgresql12-server - The programs needed to create and run a PostgreSQL server
在这里插入图片描述

YUM在线安装参考

参考地址:https://www.postgresql.org/download/linux/redhat/
在这里插入图片描述
下载地址:https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
在这里插入图片描述
在这里插入图片描述

二进制包下载

二进制文本安装

下载地址:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
在这里插入图片描述
在这里插入图片描述

绿色解压版

下载地址:https://www.enterprisedb.com/download-postgresql-binaries
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
在这里插入图片描述
这两者的区别如官网所说:
The links below allow you to download archives of the files installed by our PostgreSQL installers. These archives are provided as a convenience for expert users; unless you specifically need these files, you should download an installer instead.
除非你很需要绿色解压版文件,否则你应该使用二进制文本安装。

安装

源码包安装

解压安装包

[root@postgre ~]# cd /soft/
[root@postgre soft]# ll
total 26592
-rw-r--r--. 1 root root 27229235 Jul  8 10:39 postgresql-12.7.tar.gz
[root@postgre soft]# chown postgres:dba postgresql-12.7.tar.gz 
[root@postgre soft]# chmod 755 postgresql-12.7.tar.gz 
[root@postgre soft]# su - postgres
Last login: Thu Jul  8 12:42:05 CST 2021 on pts/0
[postgres@postgre ~]$ cd /soft/
[postgres@postgre soft]$ ll
total 26592
-rwxr-xr-x. 1 postgres dba 27229235 Jul  8 10:39 postgresql-12.7.tar.gz
[postgres@postgre soft]$ tar -zxvf postgresql-12.7.tar.gz 

编译安装

$ cd /soft/postgresql-12.7/
$ ./configure --prefix=/opt/pg12.7 --with-pgport=6000
使用make 或者make world编译,使用make install 或者make install-world 进行安装

$ make world -j 8 
当看到最后一行显示为:
PostgreSQL, contrib, and documentation successfully made. Ready to install.
说明已经编译成功

$ make install-world  
当看到最后一行显示为:
PostgreSQL, contrib, and documentation installation complete.
说明已经安装成功
$ make -j8
当看到最后一行显示为:
All of PostgreSQL successfully made. Ready to install.
说明已经编译成功
$ make install
当看到最后一行显示为:
PostgreSQL installation complete.
说明已经安装成功

说明:
1、make编译的时候可以加并行:
-j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg.
-j8和-j 8,都可以,不管中间有没有空格。

[root@postgre ~]# ps -ef|grep make
postgres  12701   8390  0 12:49 pts/0    00:00:00 make world -j 8
postgres  12860  12701  0 12:49 pts/0    00:00:00 make -C src all
postgres  13402  12860  0 12:49 pts/0    00:00:00 make -C backend all
postgres  14358  13402  0 12:49 pts/0    00:00:00 make -C nodes all
postgres  14486  13402  0 12:50 pts/0    00:00:00 make -C optimizer all
postgres  14514  14486  0 12:50 pts/0    00:00:00 make -C plan all
postgres  14625  13402  0 12:50 pts/0    00:00:00 make -C postmaster all
postgres  14662  13402  0 12:50 pts/0    00:00:00 make -C regex all
postgres  14678  14486  0 12:50 pts/0    00:00:00 make -C util all
root      14707  14282  0 12:50 pts/1    00:00:00 grep --color=auto make

会有8个并行进程
2、make install的时候不用并行,因为安装很快,只是在编译的时候耗时。
3、make world && make install-world比make && make install编译安装的东西多,从上面的结果输出就可以看出。 如果你希望编译所有能编译的东西,包括文档(html和手册页)以及附加模块(contrib),使用make world && make install-world。源码安装postgresql时,而make时又没有make world,就会导致的pg最终没有类似pg_stat_statements的扩展功能

查看版本

$ cd /opt/pg12.7/bin/
$ ./postgres --version
postgres (PostgreSQL) 12.7

设置软链接

$ cd /opt
$  ln -s /opt/pg12.7 /opt/pgsql
$ cd /opt/
$ ll
total 4
drwxr-xr-x. 1 postgres dba  36 Jul  8 13:04 pg12.7
lrwxrwxrwx. 1 postgres dba  11 Jul  8 13:14 pgsql -> /opt/pg12.7
drwxr-xr-x. 1 root     root  0 Jun 18  2018 rh

设置软连接的目的,为了使用方便,当你有升级需求时,升级完后,环境变量都不需要更改了。

yum在线安装

sudo权限配置

#visudo -f /etc/sudoers
最后一行更改:

postgres ALL = NOPASSWD:ALL

postgres用户执行所有root的命令,都不用输入密码。

安装

[postgres@source ~]$sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Loaded plugins: fastestmirror
pgdg-redhat-repo-latest.noarch.rpm                                                                                                                                 | 7.5 kB  00:00:00     
Examining /var/tmp/yum-root-vlU153/pgdg-redhat-repo-latest.noarch.rpm: pgdg-redhat-repo-42.0-18.noarch
Marking /var/tmp/yum-root-vlU153/pgdg-redhat-repo-latest.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package pgdg-redhat-repo.noarch 0:42.0-18 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================================================================
 Package                                      Arch                               Version                                Repository                                                   Size
==========================================================================================================================================================================================
Installing:
 pgdg-redhat-repo                             noarch                             42.0-18                                /pgdg-redhat-repo-latest.noarch                              11 k

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

Total size: 11 k
Installed size: 11 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : pgdg-redhat-repo-42.0-18.noarch                                                                                                                                        1/1 
  Verifying  : pgdg-redhat-repo-42.0-18.noarch                                                                                                                                        1/1 

Installed:
  pgdg-redhat-repo.noarch 0:42.0-18                                                                                                                                                       

Complete!

[postgres@source ~]$rpm -qa|grep post
postfix-2.10.1-7.el7.x86_64
[postgres@source ~]$ sudo yum install -y postgresql12-server postgresql12 postgresql12-contrib

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package postgresql12.x86_64 0:12.7-1PGDG.rhel7 will be installed
--> Processing Dependency: postgresql12-libs(x86-64) = 12.7-1PGDG.rhel7 for package: postgresql12-12.7-1PGDG.rhel7.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql12-12.7-1PGDG.rhel7.x86_64
---> Package postgresql12-contrib.x86_64 0:12.7-1PGDG.rhel7 will be installed
---> Package postgresql12-server.x86_64 0:12.7-1PGDG.rhel7 will be installed
--> Running transaction check
---> Package postgresql12-libs.x86_64 0:12.7-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================================================================
 Package                                             Arch                                  Version                                            Repository                             Size
==========================================================================================================================================================================================
Installing:
 postgresql12                                        x86_64                                12.7-1PGDG.rhel7                                   pgdg12                                1.6 M
 postgresql12-contrib                                x86_64                                12.7-1PGDG.rhel7                                   pgdg12                                611 k
 postgresql12-server                                 x86_64                                12.7-1PGDG.rhel7                                   pgdg12                                5.1 M
Installing for dependencies:
 postgresql12-libs                                   x86_64                                12.7-1PGDG.rhel7                                   pgdg12                                371 k

Transaction Summary
==========================================================================================================================================================================================
Install  3 Packages (+1 Dependent package)

Total download size: 7.6 M
Installed size: 32 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/pgdg12/packages/postgresql12-contrib-12.7-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY ]  54 kB/s | 383 kB  00:02:18 ETA 
Public key for postgresql12-contrib-12.7-1PGDG.rhel7.x86_64.rpm is not installed
(1/4): postgresql12-contrib-12.7-1PGDG.rhel7.x86_64.rpm                                                                                                            | 611 kB  00:00:04     
(2/4): postgresql12-libs-12.7-1PGDG.rhel7.x86_64.rpm                                                                                                               | 371 kB  00:00:01     
(3/4): postgresql12-12.7-1PGDG.rhel7.x86_64.rpm                                                                                                                    | 1.6 MB  00:01:49     
(4/4): postgresql12-server-12.7-1PGDG.rhel7.x86_64.rpm                                                                                                             | 5.1 MB  00:03:12     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                      39 kB/s | 7.6 MB  00:03:19     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
 Package    : pgdg-redhat-repo-42.0-18.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : postgresql12-libs-12.7-1PGDG.rhel7.x86_64                                                                                                                              1/4 
  Installing : postgresql12-12.7-1PGDG.rhel7.x86_64                                                                                                                                   2/4 
  Installing : postgresql12-server-12.7-1PGDG.rhel7.x86_64                                                                                                                            3/4 
  Installing : postgresql12-contrib-12.7-1PGDG.rhel7.x86_64                                                                                                                           4/4 
  Verifying  : postgresql12-libs-12.7-1PGDG.rhel7.x86_64                                                                                                                              1/4 
  Verifying  : postgresql12-12.7-1PGDG.rhel7.x86_64                                                                                                                                   2/4 
  Verifying  : postgresql12-server-12.7-1PGDG.rhel7.x86_64                                                                                                                            3/4 
  Verifying  : postgresql12-contrib-12.7-1PGDG.rhel7.x86_64                                                                                                                           4/4 

Installed:
  postgresql12.x86_64 0:12.7-1PGDG.rhel7                  postgresql12-contrib.x86_64 0:12.7-1PGDG.rhel7                  postgresql12-server.x86_64 0:12.7-1PGDG.rhel7                 

Dependency Installed:
  postgresql12-libs.x86_64 0:12.7-1PGDG.rhel7                                                                                                                                             

Complete!

[postgres@source ~]$rpm -qa|grep post
postgresql12-libs-12.7-1PGDG.rhel7.x86_64
postfix-2.10.1-7.el7.x86_64
postgresql12-12.7-1PGDG.rhel7.x86_64
postgresql12-server-12.7-1PGDG.rhel7.x86_64

查看安装路径

rpm有两个参数-l和-c可以帮助我们查看具体的安装路径。
-l 显示软件包中的文件列表
-c 显示配置文件列表
那么我们可以使用“rpm -ql 包名”来查看具体的安装路径。
[postgres@source ~]$rpm -qa|grep post

postgresql12-contrib-12.7-1PGDG.rhel7.x86_64
postfix-2.10.1-7.el7.x86_64
postgresql12-libs-12.7-1PGDG.rhel7.x86_64
postgresql12-server-12.7-1PGDG.rhel7.x86_64
postgresql12-12.7-1PGDG.rhel7.x86_64

[postgres@source ~]$rpm -ql postgresql12-server-12.7-1PGDG.rhel7.x86_64

/etc/pam.d/postgresql
/etc/sysconfig/pgsql
/usr/bin/postgresql-12-setup
/usr/lib/systemd/system/postgresql-12.service
/usr/lib/tmpfiles.d/postgresql-12.conf
/usr/pgsql-12/bin/initdb
/usr/pgsql-12/bin/pg_checksums
/usr/pgsql-12/bin/pg_controldata
/usr/pgsql-12/bin/pg_ctl
/usr/pgsql-12/bin/pg_resetwal
。。。。。。。。
/var/lib/pgsql/12/backups
/var/lib/pgsql/12/data
/var/run/postgresql

[postgres@source ~]$rpm -ql postgresql12
可见,postgresql安装在了目录/usr/pgsql-12下。

初始化数据库,并启动

[postgres@source bin]$ sudo /usr/pgsql-12/bin/postgresql-12-setup initdb

Initializing database ... OK

[postgres@source pgsql-12]$ sudo systemctl enable postgresql-12

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-12.service to /usr/lib/systemd/system/postgresql-12.service.

[postgres@source pgsql-12]$ sudo systemctl start postgresql-12
[postgres@source pgsql-12]$ sudo 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 2021-07-08 18:47:36 CST; 12s ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 7331 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 7336 (postmaster)
   CGroup: /system.slice/postgresql-12.service
           ├─7336 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─7338 postgres: logger   
           ├─7340 postgres: checkpointer   
           ├─7341 postgres: background writer   
           ├─7342 postgres: walwriter   
           ├─7343 postgres: autovacuum launcher   
           ├─7344 postgres: stats collector   
           └─7345 postgres: logical replication launcher   

Jul 08 18:47:36 source systemd[1]: Starting PostgreSQL 12 database server...
Jul 08 18:47:36 source postmaster[7336]: 2021-07-08 18:47:36.046 CST [7336] LOG:  starting Po...bit
Jul 08 18:47:36 source postmaster[7336]: 2021-07-08 18:47:36.048 CST [7336] LOG:  listening o...432
Jul 08 18:47:36 source postmaster[7336]: 2021-07-08 18:47:36.048 CST [7336] LOG:  listening o...432
Jul 08 18:47:36 source postmaster[7336]: 2021-07-08 18:47:36.048 CST [7336] LOG:  listening o...32"
Jul 08 18:47:36 source postmaster[7336]: 2021-07-08 18:47:36.050 CST [7336] LOG:  listening o...32"
Jul 08 18:47:36 source postmaster[7336]: 2021-07-08 18:47:36.058 CST [7336] LOG:  redirecting...ess
Jul 08 18:47:36 source postmaster[7336]: 2021-07-08 18:47:36.058 CST [7336] HINT:  Future log...g".
Jul 08 18:47:36 source systemd[1]: Started PostgreSQL 12 database server.
Hint: Some lines were ellipsized, use -l to show in full.

[postgres@source pgsql-12]$ ps -ef|grep post

root       6798      1  0 18:13 ?        00:00:00 /usr/libexec/postfix/master -w
postfix    6808   6798  0 18:13 ?        00:00:00 pickup -l -t unix -u
postfix    6809   6798  0 18:13 ?        00:00:00 qmgr -l -t unix -u
root       6948   6909  0 18:20 pts/0    00:00:00 su - postgres
postgres   6949   6948  0 18:20 pts/0    00:00:00 -bash
postgres   7336      1  0 18:47 ?        00:00:00 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
postgres   7338   7336  0 18:47 ?        00:00:00 postgres: logger   
postgres   7340   7336  0 18:47 ?        00:00:00 postgres: checkpointer   
postgres   7341   7336  0 18:47 ?        00:00:00 postgres: background writer   
postgres   7342   7336  0 18:47 ?        00:00:00 postgres: walwriter   
postgres   7343   7336  0 18:47 ?        00:00:00 postgres: autovacuum launcher   
postgres   7344   7336  0 18:47 ?        00:00:00 postgres: stats collector   
postgres   7345   7336  0 18:47 ?        00:00:00 postgres: logical replication launcher   
postgres   7368   6949  0 18:47 pts/0    00:00:00 ps -ef
postgres   7369   6949  0 18:47 pts/0    00:00:00 grep --color=auto post

[postgres@source pgsql-12]$ ps -ef|grep pgsql
postgres 7336 1 0 18:47 ? 00:00:00 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
postgres 7371 6949 0 18:48 pts/0 00:00:00 grep --color=auto pgsql

拿root用户安装

事先不创建任何用户,不创建任何目录,执行如下操作:
[root@source ~]安装部署postgresql-15 高可用(pgpool)

PostgreSQL on Azure.cn : 安装部署

postgresql9.3.9版本部署

postgresql-10.12安装和部署

Postgresql服务部署

Babelfish for PostgreSQL 安装部署