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

embedded/2025/4/1 4:07:21/

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
getenforcePermissive   –输出(表示临时关闭)
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 ... okinitdb: 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=notifyUser=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=0TimeoutStopSec=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.servicepostgresql-16.service - PostgreSQL 16 database serverLoaded: 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 agoDocs: https://www.postgresql.org/docs/16/static/Main PID: 1062 (postgres)Tasks: 6 (limit: 37638)Memory: 11.9MCGroup: /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 连接配置文件

[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

http://www.ppmy.cn/embedded/176718.html

相关文章

游戏MOD伴随盗号风险,仿冒网站借“风灵月影”窃密【火绒企业版V2.0】

游戏MOD&#xff08;即游戏修改器&#xff09;是一种能够对游戏进行修改或增强的程序&#xff0c;因其能够提升游戏体验&#xff0c;在玩家群体中拥有一定的市场。然而&#xff0c;这类程序大多由第三方开发者制作&#xff0c;容易缺乏完善的安全保障机制&#xff0c;这就为不法…

如何在Linux CentOS上安装和配置Redis

如何在Linux CentOS上安装和配置Redis 大家好&#xff0c;我是曾续缘。欢迎来到本教程&#xff01;今天我将向您介绍在Linux CentOS上安装和配置Redis的详细步骤。Redis是一个高性能的键值存储系统&#xff0c;常用于缓存、消息队列和数据持久化等应用场景。让我们一起开始吧&…

NVIDIA TensorRT-LLM:高性能大语言模型推理框架详解

NVIDIA TensorRT-LLM&#xff1a;高性能大语言模型推理框架详解 引言 随着大语言模型&#xff08;LLMs&#xff09;在各行各业的应用日益广泛&#xff0c;如何高效地部署这些模型以实现低延迟、高吞吐量的推理服务成为了一个关键挑战。NVIDIA TensorRT-LLM 是 NVIDIA 专为大语…

UDP通信实现

一、Socket简介&#xff08;套接字) TCP/IP 五层网络模型的应用层编程接口称为Socket API, Socket( 套接字 ) &#xff0c;它是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。 一个套接字就是网络上进程通信的一端&#xff0c;提供了应用层进程利用网络协议交换…

MongoDB 面试备战指南

MongoDB 面试备战指南 一、基础概念 1. MongoDB是什么类型的数据库&#xff1f;和关系型数据库有什么区别&#xff1f; 答案&#xff1a; MongoDB是文档型NoSQL数据库&#xff0c;核心区别&#xff1a; 数据模型&#xff1a;存储JSON-like文档&#xff08;动态schema&#xf…

玄机-第六章 流量特征分析-蚂蚁爱上树的测试报告

目录 一、测试环境 二、测试目的 三、操作过程 Flag1 Flag2 Flag3 四、结论 一、测试环境 靶场介绍&#xff1a;国内厂商设置的玄机靶场&#xff0c;以应急响应题目著名。 地址&#xff1a;https://xj.edisec.net/challenges/44 靶机简介&#xff1a; 二、测试目的 …

基于 arco 的 React 和 Vue 设计系统

arco 是字节跳动出品的企业级设计系统&#xff0c;支持React 和 Vue。 安装模板工具 npm i -g arco-cli创建项目目录 cd someDir arco init hello-arco-pro? 请选择你希望使用的技术栈React❯ Vue? 请选择一个分类业务组件组件库Lerna Menorepo 项目❯ Arco Pro 项目看到以…

Apache SeaTunnel同步MySQL到Doris的优化策略

在数据仓库建设过程中&#xff0c;数据同步是一个关键环节。Apache SeaTunnel作为一个高性能的分布式数据集成工具&#xff0c;被广泛用于将MySQL数据同步到Doris等OLAP数据库。 然而&#xff0c;如何优化这个同步过程&#xff0c;提高效率并减少资源消耗&#xff0c;是每个数据…