PG源码编译anzuang

系统资源限制设置

vi /etc/security/limits.conf

* soft nofile  1024000
* hard nofile  1024000
* soft noproc  1024000
* hard noproc  1024000
* soft core    1024000
* hard core    1024000
* soft memlock 1024000
* hard memlock 1024000
vi /etc/security/limits.d/20-nproc.conf

*          soft    nproc     unlimited
root       soft    nproc     unlimited

SELINUX和防火墙设置

vi /etc/sysconfig/selinux

SELINUX=disabled
SELINUXTYPE=targeted

OS防火墙(建议按业务场景设置,不需要就先删除所有策略,并关闭服务)

方法1

#设置默认接受所有请求
/sbin/iptables -P INPUT ACCEPT
#清除防火墙规则
/sbin/iptables -F

方法2

关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service

创建组及用户,并设置密码

groupadd postgres
useradd -g postgres -m postgres
passwd postgres

源码包下载

https://www.postgresql.org/ftp/source/v12.2/
postgresql-12.2.tar.gz

检查依赖包

rpm -q openssl  openssl-devel  pam  pam-devel  libxml2  libxml2-devel  libxslt  libxslt-devel  perl  perl-devel  python-devel  perl-ExtUtils-Embed  readline  readline-devel  zlib  zlib-devel  gettext  gettext-devel  bison  flex  uuid-devel  gcc  gcc-c++ \systemd

配置yum源参考

https://www.cnblogs.com/connected/p/12642029.html

安装依赖包

yum install -y openssl openssl-devel  pam  pam-devel  libxml2  libxml2-devel  libxslt  libxslt-devel  perl  perl-devel  python-devel  perl-ExtUtils-Embed  readline  readline-devel  zlib  zlib-devel  gettext  gettext-devel  bison  flex  uuid-devel  gcc  gcc-c++ \systemd*

上传源码包到/pg-rpm

[6 ~]# mkdir /pg-rpm

解压并设置权限

cd /pg-rpm/
tar -zxvf postgresql-12.2.tar.gz
chown -R postgres:postgres postgresql-12.2

创建pg安装目录

mkdir -p /opt/pg12
chown postgres:postgres /opt/pg12

切换用户

su - postgres

编译安装

cd /pg-rpm/postgresql-12.2

./configure --prefix=/opt/pg12 --with-pgport=9527 --with-openssl  --with-perl --with-python --with-blocksize=16 \--with-systemd

如果加上“--with-systemd”后报错如下:

configure: error: header file <systemd/sd-daemon.h> is required for systemd support

则需要检查是否安装了systemd相关包,在这次验证中缺少的是 systemd-devel-219-62.el7.x86_64 

参数详解

---------------
./configure --prefix=PREFIX
把所有文件装在目录PREFIX中而不是/usr/local/pgsql中。 
实际的文件会安装到数个子目录中;没有一个文件会直接安装到PREFIX根目录里。
结果如下:
[-6 ~]$ ls  /opt/pg12/
bin  include  lib  share
[-6 ~]$


--with-pgport=NUMBER
把NUMBER设置为服务器和客户端的默认端口。默认是 5432。 这个端口可以在以后修改,不过如果你在这里声明,那么服务器和客户端将有相同的编译好了的默认值。这样会非常方便些。 通常选取一个非默认值的理由是你企图在同一台机器上运行多个PostgreSQL服务器。

--with-openssl
编译SSL(加密)连接支持。这个选项需要安装OpenSSL包。configure将会检查所需的头文件和库以确保你的 OpenSSL安装足以让配置继续下去。

--with-perl
制作PL/Perl服务器端编程语言。

--with-python
制作PL/Python服务器端编程语言。

--with-blocksize=BLOCKSIZE
设置块尺寸,以 K 字节计。这是表内存储和I/O的单位。默认值(8K字节)适合于大多数情况,但是在特殊情况下可能其他值更有用。这个值必须是2的幂并且在 1 和 32 (K字节)之间。注意修改这个值需要一次 initdb。
--with-systemd系统服务方式管理
---------------

编译

make world

执行完成后显示的最后一行应该是:
PostgreSQL, contrib, and documentation successfully made. Ready to install.

注:
源码安装postgresql时,而make时又没有make world,就会导致的pg最终没有类似pg_stat_statements的扩展功能
如果选择make ,后续手动安装pg扩展插件,请参考https://www.cnblogs.com/ejQiu-123/p/11497362.html
make
(一定要记得用GNU make)。依你的硬件而异,编译过程可能需要 5 分钟到半小时。显示的最后一行应该是:

如果你希望编译所有能编译的东西,包括文档(HTML和手册页)以及附加模块(contrib),这样键入:
make world

安装PostgreSQL

make install-world

执行完成后显示的最后一行应该是:
PostgreSQL, contrib, and documentation installation complete.

查看安装后的目录结构

[6 ~]$ ls  /opt/pg12/
bin  include  lib  share
[-6 ~]$

创建数据存放目录并设置权限

[6 ~]$ mkdir /opt/pg12/data
[ ~]$ chown postgres:postgres /opt/pg12/data

配置环境变量

[6 ~]$ vi ~/.bash_profile

export PGHOME=/opt/pg12
export PGPORT=9527
export PGDATA=/opt/pg12/data
export LD_LIBRARY_PATH=$PGHOME/lib:LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
export PATH=/usr/local/pgsql/bin:$PATH:.

生效环境变量

source ~/.bash_profile

创建一个数据库集簇

[6 ~]$ initdb -D /opt/pg12/data -E UTF8 --locale=zh_CN.utf8
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.utf8".
initdb: could not find suitable text search configuration for locale "zh_CN.utf8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /opt/pg12/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /opt/pg12/data -l logfile start

[-6 ~]$

启动

[6 ~]$ pg_ctl start
waiting for server to start....2020-04-13 16:44:34.479 CST [40486] LOG:  starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
2020-04-13 16:44:34.485 CST [40486] LOG:  listening on IPv6 address "::1", port 9527
2020-04-13 16:44:34.485 CST [40486] LOG:  listening on IPv4 address "127.0.0.1", port 9527
2020-04-13 16:44:34.486 CST [40486] LOG:  listening on Unix socket "/tmp/.s.PGSQL.9527"
2020-04-13 16:44:34.499 CST [40487] LOG:  database system was shut down at 2020-04-13 16:42:37 CST
2020-04-13 16:44:34.503 CST [40486] LOG:  database system is ready to accept connections
 done
server started
[-6 ~]$

验证安装情况

[6 ~]$ psql -Upostgres -l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | zh_CN.utf8 | zh_CN.utf8 |
 template0 | postgres | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

相关推荐