当前位置: 首页 > news >正文

PostgreSQL 数据库源码编译安装全流程详解 Linux 8

PostgreSQL 数据库源码编译安装全流程详解 Linux 8

  • 1. 基础环境配置
    • 1.1 修改主机名
    • 1.2 配置操作系统yum源
    • 1.3 安装操作系统依赖包
    • 1.4 禁用SELINUX配置
    • 1.5 关闭操作系统防火墙
    • 1.6 创建用户和组
    • 1.7 建立安装目录
    • 1.8 编辑环境变量
  • 2. 源码方式安装(PG 16)
    • 2.1 下载源码安装包
    • 2.2 编译安装源码包
    • 2.3 初始化数据库
    • 2.4 启动数据库
    • 2.5 开机自启动
    • 2.6 编辑环境变量
  • 3. 安装后配置
    • 3.1 修改默认用户postgres密码
    • 3.2 连接配置文件
    • 3.3 数据库启动关闭

1. 基础环境配置

1.1 修改主机名

hostnamectl set-hostname pgdb
# 配置hosts文件
cat >> /etc/hosts <<EOF
192.168.1.109   pgdb
EOF

1.2 配置操作系统yum源

创建挂载目录
mkdir /mnt/iso
编辑yum源配置文件
cat > /etc/yum.repos.d/yum.repo<<EOF
[sourceOS]
name=yumserverOS
baseurl=file:///mnt/iso/BaseOS
gpgcheck=0
enabled=1
 
[sourceAPP]
name=yumserverApp
baseurl=file:///mnt/iso/AppStream
gpgcheck=0
enabled=1
EOF
检查yum源
yum clean all
yum makecache
yum repolist

1.3 安装操作系统依赖包

yum install -y mke automake zlib zlib-devel bzip2 bzip2-devel bzip2-libs readline readline-devel gcc gcc-c++ bison ncurses ncurses-devel libaio-devel gmp gmp-devel mpfr mpfr-devel libmpc 

1.4 禁用SELINUX配置

getenforce 
setenforce 0 
iptables -F
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
cat /etc/selinux/config
getenforce
 
Permissive   –输出(表示临时关闭)
Disabled     –重启服务器后输出

1.5 关闭操作系统防火墙

systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service

1.6 创建用户和组

# 创建pg用户和组
groupadd -g 2000 postgres
useradd -g postgres -u 2000 postgres
echo "postgres123" | passwd --stdin postgres

# 验证创建成功
id postgres

1.7 建立安装目录

# 创建安装目录
mkdir -p /pgsql/app/pg16
mkdir -p /pgsql/soft
mkdir -p /pgdata/data/pg16
mkdir -p /pgdata/arch
chown -R postgres.postgres /pgsql
chown -R postgres.postgres /pgdata
chmod -R 775 /pgsql
chmod -R 775 /pgdata

1.8 编辑环境变量

# 编辑环境变量
vi /home/postgres/.bash_profile
export PGPORT=5432
export PGDATA=/pgdata/data/pg16
export PGHOME=/pgsql/app/pg16
export LANG=en_US.utf8
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
export PGDATABASE=postgres

2. 源码方式安装(PG 16)

2.1 下载源码安装包

官方下载地址:
https://www.postgresql.org/ftp/source/

[root@pgdb soft]# ll
total 31676
-rwxr-xr-x 1 root root 32433767 Mar 19 14:23 postgresql-16.1.tar.gz

2.2 编译安装源码包

# 解压源码包
[root@pgdb soft]# tar -xvf postgresql-16.1.tar.gz
[root@pgdb soft]# cd postgresql-16.1
# 配置编译选项 可以自定义端口号 默认5432
[root@pgdb postgresql-16.1]# ./configure --prefix=/pgsql/app/pg16 --with-pgport=5436
# 编译源代码
[root@pgdb postgresql-16.1]# make
# 编译安装
[root@pgdb postgresql-16.1]# make install

2.3 初始化数据库

[postgres@pgdb ~]$  /pgsql/app/pg16/bin/initdb -D /pgdata/data/pg16 
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 "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /pgdata/data/pg16 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
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
initdb: hint: 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 /pgdata/data/pg16 -l logfile start

2.4 启动数据库

[postgres@pgdb ~]$ pg_ctl -D /pgdata/data/pg16 -l logfile start
waiting for server to start.... done
server started

2.5 开机自启动

  • 添加配置文件 参考PG16 RPM安装时配置文件
  • Environment=PGDATA=/pgdata/data/pg16/
  • ExecStart=/pgsql/app/pg16/bin/postgres -D ${PGDATA}

vi /usr/lib/systemd/system/postgresql-16.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  It is recommended to use systemd
# "dropin" feature;  i.e. create file with suffix .conf under
# /etc/systemd/system/postgresql-16.service.d directory overriding the
# unit's defaults. You can also use "systemctl edit postgresql-16"
# Look at systemd.unit(5) manual page for more info.

# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-16-setup.
[Unit]
Description=PostgreSQL 16 database server
Documentation=https://www.postgresql.org/docs/16/static/
After=syslog.target
After=network-online.target

[Service]
Type=notify

User=postgres
Group=postgres

# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.

# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/16/data/
Environment=PGDATA=/pgdata/data/pg16/

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on postgres main process
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0

#ExecStartPre=/usr/pgsql-16/bin/postgresql-16-check-db-dir ${PGDATA}
ExecStart=/pgsql/app/pg16/bin/postgres -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
 
# Do not set any timeout value, so that systemd will not kill postgres 
# main process during crash recovery.
TimeoutSec=0

# 0 is the same as infinity, but "infinity" needs systemd 229
TimeoutStartSec=0

TimeoutStopSec=1h

[Install]
WantedBy=multi-user.target

  • 重新加载配置文件 systemctl start postgresql-16.service 不会结束命令,systemctl status postgresql-16.service Active: activating (start)显示状态非running
# reload配置文件
[root@pgdb ~]# systemctl daemon-reload
# 启动数据库
[root@pgdb ~]# systemctl start postgresql-16.service
# 开机自启动
[root@pgdb ~]# systemctl enable postgresql-16.service
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-16.service → /usr/lib/systemd/system/postgresql-16.service.
# pg14 资源状态
[root@pgdb ~]# systemctl status postgresql-16.service
● postgresql-16.service - PostgreSQL 16 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-16.service; enabled; vendor preset: disabled)
   Active: activating (start) since Wed 2025-03-19 16:21:17 CST; 3min 33s ago
     Docs: https://www.postgresql.org/docs/16/static/
 Main PID: 1062 (postgres)
    Tasks: 6 (limit: 37638)
   Memory: 11.9M
   CGroup: /system.slice/postgresql-16.service
           ├─1062 /pgsql/app/pg16/bin/postgres -D /pgdata/data/pg16/
           ├─1067 postgres: checkpointer 
           ├─1068 postgres: background writer 
           ├─1070 postgres: walwriter 
           ├─1071 postgres: autovacuum launcher 
           └─1072 postgres: logical replication launcher 

Mar 19 16:21:17 pgdb systemd[1]: Starting PostgreSQL 16 database server...
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.178 CST [1062] LOG:  starting PostgreSQL 16.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.4.1 20200928 (Re>
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.179 CST [1062] LOG:  listening on IPv6 address "::1", port 5436
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.179 CST [1062] LOG:  listening on IPv4 address "127.0.0.1", port 5436
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.180 CST [1062] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5436"
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.184 CST [1069] LOG:  database system was shut down at 2025-03-19 16:19:41 CST
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.187 CST [1062] LOG:  database system is ready to accept connections

2.6 编辑环境变量

#PG 16
######
export PGPORT=5436
export PGDATA=/pgdata/data/pg16
export PGHOME=/pgsql/app/pg16

3. 安装后配置

3.1 修改默认用户postgres密码

postgres=# alter user postgres with password 'postgres';
ALTER ROLE

3.2 连接配置文件

  • postgresql.conf参数 监听所有地址IP
[postgres@pgdb pg16]$ cd $PGDATA
[postgres@pgdb pg16]$ vi postgresql.conf
#listen_addresses = 'localhost'         # what IP address(es) to listen on;  #重启生效
listen_addresses = '*'
  • pg_hba.conf参数 实例访问控制
[postgres@pgdb pg16]$ cd $PGDATA
[postgres@pgdb pg16]$ vi pg_hba.conf
# IPv4 local connections:
#host    all             all             127.0.0.1/32            trust   #重加载配置生效 
# 允许192.168.1.0网段连接数据库
host    all             all             192.168.1.0/24          scram-sha-256
  • 重启数据库
[postgres@pgdb ~]$ pg_ctl restart
  • 远程连接数据库
[postgres@pgdb ~]$ psql -h 192.168.1.109 -p 5436 -U postgres -d postgres

3.3 数据库启动关闭

[postgres@pgdb ~]$ pg_ctl status
pg_ctl: server is running (PID: 1063)
/pgsql/app/pg16/bin/postgres "-D" "/pgdata/data/pg16/"
[postgres@pgdb ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@pgdb ~]$ pg_ctl start
waiting for server to start....2025-03-21 22:24:33.069 CST [1335] LOG:  redirecting log output to logging collector process
2025-03-21 22:24:33.069 CST [1335] HINT:  Future log output will appear in directory "log".
 done
server started
[postgres@pgdb ~]$ pg_ctl status
pg_ctl: server is running (PID: 1335)
/pgsql/app/pg16/bin/postgres

相关文章:

  • SysVinit和Systemd的系统运行级别
  • 探秘 WRF DA:多维度剖析其在气象研究中的卓越效能
  • C++具名转型的功能和用途
  • 奇怪的异形选项卡样式、弧形边框选项卡
  • Go语言中package的使用规则《二》
  • java基础之windows电脑基础命令
  • 【其他】在线安装DataEase后无法远程访问
  • k近邻图(knn-graph)和局部线性嵌入图(LLE-graph)的相似性和区别
  • Python JSON模块loads、load、dump、dumps详解
  • Sql Server 索引性能优化 分析以及分表
  • 【LeetCode】大厂面试算法真题回忆(37)--知识图谱新词挖掘
  • 大数据从入门到入魔系列————探索大数据前世今生之迷
  • Unity | 游戏数据配置
  • SpringBoot整合MQTT最详细版(亲测有效)
  • 创建自己的github.io
  • Jmeter插件下载和配置
  • 终端的命令行发送邮件的方式和监视脚本
  • windows专用网路的共享文件配置
  • EtherCAT转profinet网关集成汽车变速箱制造生产线自动化升级
  • 高频GNSS同震形变计算方法
  • 吴志朴当选福建德化县人民政府县长
  • 屠呦呦当选美国科学院外籍院士
  • 黄育奇当选福建惠安县人民政府县长
  • 张炜琳已任三明市委常委、宣传部部长
  • “人工智能是年轻的事业,也是年轻人的事业”,沪上高校师生畅谈感想
  • 交行一季度净利253.72亿元增1.54%,不良率微降