Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库

源码编译安装适合有特殊需求的环境安装,可以选择特定的功能编译然后安装,也适合在独立的内网环境安装;

源码包下载地址:

https://www.postgresql.org/ftp/source/

# 15.7下载链接
https://ftp.postgresql.org/pub/source/v15.7/postgresql-15.7.tar.gz
图片[1]-Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库-李佳程的个人主页

关闭防火墙和SELinux

# 设置防火墙开机不启动
[root@centos7 ~]# systemctl disable firewalld.service 

# 关闭防火墙
[root@centos7 ~]# systemctl stop firewalld.service

# 关闭 selinux
[root@centos7 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

内核参数优化

[root@centos7 ~]# vi /etc/sysctl.conf
kernel.shmmax = 68719476736 
kernel.shmall = 4294967296 
kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 7672460
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576

[root@centos7 ~]# sysctl -p

[root@centos7 ~]# vi /etc/security/limits.conf
* - nofile 100000
* - nproc 100000
* - memlock 60000

安装依赖包

[root@centos7 ~]# yum install -y gcc make readline-devel zlib-devel

源码编译安装

# 下载源码包
[root@centos7 ~]# wget https://ftp.postgresql.org/pub/source/v15.7/postgresql-15.7.tar.gz

# 解压缩源码包
[root@centos7 ~]# tar xf postgresql-15.7.tar.gz 

# 查看解压缩后的包
[root@centos7 ~]# cd postgresql-15.7/
[root@centos7 postgresql-15.7]# ll
total 776
-rw-r--r--  1 1107 1107    397 May  7 04:23 aclocal.m4
drwxrwxrwx  2 1107 1107   4096 May  7 04:34 config
-rwxr-xr-x  1 1107 1107 602097 May  7 04:23 configure
-rw-r--r--  1 1107 1107  89356 May  7 04:23 configure.ac
drwxrwxrwx 61 1107 1107   4096 May  7 04:34 contrib
-rw-r--r--  1 1107 1107   1192 May  7 04:23 COPYRIGHT
drwxrwxrwx  3 1107 1107     87 May  7 04:34 doc
-rw-r--r--  1 1107 1107   4264 May  7 04:23 GNUmakefile.in
-rw-r--r--  1 1107 1107    277 May  7 04:23 HISTORY
-rw-r--r--  1 1107 1107  63842 May  7 04:35 INSTALL
-rw-r--r--  1 1107 1107   1875 May  7 04:23 Makefile
-rw-r--r--  1 1107 1107   1213 May  7 04:23 README
drwxrwxrwx 16 1107 1107    328 May  7 04:35 src
图片[2]-Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库-李佳程的个人主页

查看安装说明:

[root@centos7 postgresql-15.7]# head -20 INSTALL 

Short Version

    ./configure
    make
    su
    make install
    adduser postgres
    mkdir -p /usr/local/pgsql/data
    chown postgres /usr/local/pgsql/data
    su - postgres
    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
    /usr/local/pgsql/bin/createdb test
    /usr/local/pgsql/bin/psql test
图片[3]-Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库-李佳程的个人主页

开始编译安装

# 指定安装目录和端口
[root@centos7 postgresql-15.7]# ./configure --prefix=/apps/pgsql --with-pgport=5432  
图片[4]-Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库-李佳程的个人主页
# 默认 make 不包括文档和其它模块,2表示当产主机的CPU核心数
[root@centos7 postgresql-15.7]# make -j 2 world

# 默认 make install 不包括安装文档
[root@centos7 postgresql-15.7]# make install-world

创建数据库用户和组

PostgreSQL默认不支持以 root 启动数据库,可以通过修改源码实现root启动,但不建议这样操作,安全性较低,因此需要创建一个用于启动数据库的普通用户。

# 创建数据库用户和组,注意此用户需要可以交互登录
[root@centos7 postgresql-15.7]# useradd -s /bin/bash -m -d /home/postgres postgres

# 修改postgres密码
[root@centos7 postgresql-15.7]# echo postgres:123456|chpasswd
# 也可以通过 passwd postgres 来修改

创建数据目录并授权

# 创建目录
[root@centos7 ~]# mkdir -pv /pgsql/data

# 授权
[root@centos7 ~]# chown postgres.postgres /pgsql/data

设置环境变量

[root@centos7 ~]# cat /etc/profile.d/pgsql.sh
export PGHOME=/apps/pgsql
export PATH=$PGHOME/bin/:$PATH
export PGDATA=/pgsql/data
export PGUSER=postgres
export MANPATH=/apps/pgsql/share/man:$MANPATH

验证环境变量是否生效

图片[5]-Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库-李佳程的个人主页

初始化数据库

[postgres@centos7 ~]$ initdb -A md5 -D $PGDATA -E utf8 --locale=C -U postgres -W
-A #指定local connections默认的身份验证方法
-D #指定数据目录
-E #指定字符集
--locale=C #指定语言环境
-U  #指定数据库superuser用户名
-W  #指定数据库superuser的用户密码
图片[6]-Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库-李佳程的个人主页

启动和关闭数据库

# 启动数据库
[postgres@centos7 ~]$ pg_ctl -D /pgsql/data -l logfile start

# 进入数据库
[postgres@centos7 ~]$ psql 

# 查看登录的数据库用户
postgres=# \c

# 查看数据库版本
postgres=# select version();

# 查看数据库
postgres=# \l

# 停止数据库
[postgres@centos7 ~]$ pg_ctl stop -D /pgsql/data
图片[7]-Centos7.9 通过源码编译安装 PostgreSQL 15.7 数据库-李佳程的个人主页

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享