PostgreSQL 安装部署

Posted P10ZHUO

tags:

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

PostgreSQL 安装部署

安装前准备

文件系统建议使用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


这两者的区别如官网所说:
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在线安装

由于安装用户的不同,安装文件的权限属组,数据库的权限和属组都不相同,从以下几个方面测试:
1、 普通用户安装
1)postgres sudo用户安装
2)非postgres sudo用户安装

2、root用户安装
1)不创建postgres用户安装
2)创建postgres(但是用户名可以变下,如pg)用户安装

观察:1、启动进程的用户
2、安装目录的属组
3、数据库目录的属组
4、环境变量
5、启停的用户

postgres sudo用户安装

  1. sudo权限配置
    #visudo -f /etc/sudoers
    最后一行更改:
postgres ALL = NOPASSWD:ALL

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

  1. 新建用户

#groupadd dba -g 1001
#useradd -g dba -u 1001 -d /home/postgres -m -s /bin/bash postgres
#echo “postgres” | passwd --stdin postgres

  1. 安装
    #su - postgres
    $id postgres
uid=1001(postgres) gid=1001(dba) groups=1001(dba)

[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-cpJyrE/pgdg-redhat-repo-latest.noarch.rpm: pgdg-redhat-repo-42.0-18.noarch
Marking /var/tmp/yum-root-cpJyrE/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
epel/x86_64/metalink                                                                                                                                               | 9.1 kB  00:00:00     
 * base: mirrors.aliyun.com
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                               | 3.6 kB  00:00:00     
debuginfo                                                                                                                                                          | 2.5 kB  00:00:00     
epel                                                                                                                                                               | 4.7 kB  00:00:00     
extras                                                                                                                                                             | 2.9 kB  00:00:00     
pgdg-common/7/x86_64/signature                                                                                                                                     |  198 B  00:00:00     
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
pgdg-common/7/x86_64/signature                                                                                                                                     | 2.9 kB  00:00:00 !!! 
pgdg10/7/x86_64/signature                                                                                                                                          |  198 B  00:00:00     
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
pgdg10/7/x86_64/signature                                                                                                                                          | 3.6 kB  00:00:00 !!! 
pgdg11/7/x86_64/signature                                                                                                                                          |  198 B  00:00:00     
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
pgdg11/7/x86_64/signature                                                                                                                                          | 3.6 kB  00:00:00 !!! 
pgdg12/7/x86_64/signature                                                                                                                                          |  198 B  00:00:00     
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
pgdg12/7/x86_64/signature                                                                                                                                          | 3.6 kB  00:00:00 !!! 
pgdg13/7/x86_64/signature                                                                                                                                          |  198 B  00:00:00     
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
pgdg13/7/x86_64/signature                                                                                                                                          | 3.6 kB  00:00:00 !!! 
pgdg96/7/x86_64/signature                                                                                                                                          |  198 B  00:00:00     
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
pgdg96/7/x86_64/signature                                                                                                                                          | 3.6 kB  00:00:00 !!! 
updates                                                                                                                                                            | 2.9 kB  00:00:00     
(1/18): epel/x86_64/updateinfo                                                                                                                                     | 1.0 MB  00:00:00     
(2/18): epel/x86_64/group_gz                                                                                                                                       |  96 kB  00:00:00     
(3/18): extras/x86_64/primary_db                                                                                                                                   | 242 kB  00:00:01     
(4/18): epel/x86_64/primary_db                                                                                                                                     | 6.9 MB  00:00:01     
pgdg10/7/x86_64/group_gz       FAILED                                          36% [========================-                                           ] 2.1 MB/s | 9.9 MB  00:00:08 ETA 
https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/repodata/2cd836c03a9a90fb5dadc7e588362f26da171bcb8ae6eac9b2e75744c4f9c926-postgresqldbserver-10.xml.gz: [Errno 14] curl#35 - "TCP connection reset by peer"
Trying other mirror.
pgdg11/7/x86_64/group_gz       FAILED                                          
https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/repodata/b7d0e8818c335fe56713a8c5569b7c43af195739ccbeb25405acf9cf973ef37d-postgresqldbserver-11.xml.gz: [Errno 14] curl#35 - "TCP connection reset by peer"
Trying other mirror.
pgdg11/7/x86_64/primary_db     FAILED                                          37% [=========================                                           ] 2.1 MB/s |  10 MB  00:00:08 ETA 
https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/repodata/fac57968e172d0ad6b17641e6fb7382a410984724a563cf66d40e10c9436937d-primary.sqlite.bz2: [Errno 14] curl#35 - "TCP connection reset by peer"
Trying other mirror.
pgdg12/7/x86_64/group_gz       FAILED                                          38% [==========================                                          ] 2.0 MB/s |  10 MB  00:00:08 ETA 
https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/repodata/321321fb85e82fa8974da1255ae28b1c1355e4c9697ac8cfa2b64c5f93c711eb-postgresqldbserver-12.xml.gz: [Errno 14] curl#35 - "TCP connection reset by peer"
Trying other mirror.
(5/18): pgdg12/7/x86_64/primary_db                                                                                                                                 | 209 kB  00:00:02     
(6/18): pgdg13/7/x86_64/group_gz                                                                                                                                   |  246 B  00:00:00     
(7/18): pgdg13/7/x86_64/primary_db                                                                                                                                 | 119 kB  00:00:01     
(8/18): pgdg96/7/x86_64/group_gz                                                                                                                                   |  249 B  00:00:00     
(9/18): base/x86_64/primary_db                                                                                                                                     | 6.1 MB  00:00:07     
(10/18): pgdg-common/7/x86_64/primary_db                                                                                                                           | 133 kB  00:00:08     
(11/18): pgdg96/7/x86_64/primary_db                                                                                                                                | 321 kB  00:00:02     
(12/18): pgdg10/7/x86_64/primary_db                                                                                                                                | 331 kB  00:00:12     
(13/18): updates/x86_64/primary_db                                                                                                                                 | 8.8 MB  00:00:10     
(14/18): debuginfo/x86_64/primary_db                                                                                                                               | 2.6 MB  00:01:19     
pgdg11/7/x86_64/primary_db                                                                                                                                         | 351 kB  00:00:58     
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                               

以上是关于PostgreSQL 安装部署的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL 安装部署

安装部署postgresql-15 高可用(pgpool)

thingsboard在window部署

thingsboard在window部署

thingsboard在window部署

thingsboard在window部署