运维栈

  • 首页
  • 网络和安全
    • 网络
    • 安全
  • 操作系统
    • Linux
  • 中间件
    • Nginx
  • 数据库
    • MySQL
    • PostgreSQL
  • 云计算
    • kvm
    • 容器
    • K8S
  • 编程
    • Golang
    • Python
  • 运维
    • zabbix
    • prometheus
  • 人工智能
运维栈
一个专注于分享运维技术的小站
  1. 首页
  2. 数据库
  3. PostgreSQL
  4. 正文

RockyLinux9.5源码安装postgresql17

2025年5月15日 55点热度 0人点赞 0条评论

文章目录[隐藏]

  • 一、什么是 Postgresql
  • 二、环境
  • 三、安装步骤
    • 1、安装依赖环境
    • 2、调整内核系统参数
    • 3、下载并解压源码文件
    • 4、配置编译参数
    • 5、编译安装
    • 6、创建用户并修改权限
    • 7、配置postgres用户环境变量(使用postgres用户操作)
    • 8、初始化数据库(使用postgres用户)
    • 9、使用 systemd 管理 postgres
    • 10、修改 postgres 数据库配置文件(可选)
    • 11 、设置 postgres 数据库开机启动

一、什么是 Postgresql

PostgreSQL 是一款功能全面且开源的关系型数据库管理系统,凭借其卓越的扩展能力和对SQL标准的严格遵循而广受赞誉。作为一款成熟的数据库系统,它不仅支持符合ACID特性的事务处理,还集成了自动更新的视图、物化视图、触发器、外键约束以及存储过程等一系列强大功能。PostgreSQL能够在Windows、Linux、macOS等主流操作系统上流畅运行,其应用范围极为广泛,无论是单机应用、大规模数据仓库,还是数据湖、高并发Web服务等场景,都能应对自如。

二、环境

  • 系统: RockyLinux9.5
  • 数据库: Postgresql 17.2

三、安装步骤

注意: 以下所有步骤除明确说明外均使用 root 用户(或者具有root权限的用户)。

1、安装依赖环境

dnf install -y systemtap-sdt-devel  readline-devel zlib-devel openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel gettext tcl-devel python3-devel perl-devel gcc libicu libicu-devel bison flex perl-CPAN

cpan ExtUtils::Embed ExtUtils::MakeMaker
dnf install -y perl-ExtUtils-Embed perl-ExtUtils-MakeMaker

2、调整内核系统参数

cat >> /etc/sysctl.conf << EOF
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
EOF

echo >> /etc/security/limits.conf << EOF
*       soft    nofile  131072
*       hard    nofile  131072
*       soft    nproc   131072
*       hard    nproc   131072
*       soft    core    unlimited
*       hard    core    unlimited
*       soft    memlock 50000000
*       hard    memlock 50000000
EOF

3、下载并解压源码文件

# 官方下载链接,如果下载速度慢可以考虑使用下面的国内镜像站下载链接
wget -c https://ftp.postgresql.org/pub/source/v17.2/postgresql-17.2.tar.gz

# 国内镜像站下载链接,
wget -c https://mirrors.ustc.edu.cn/postgresql/source/v17.2/postgresql-17.2.tar.gz

tar -xf postgresql-17.2.tar.gz
cd postgresql-17.2

4、配置编译参数

./configure \
  --prefix=/oms/postgres17.2 \
  --enable-nls='en_US zh_CN' \
  --with-perl \
  --with-python \
  --with-tcl \
  --with-icu \
  --with-openssl \
  --with-ldap \
  --with-pam \
  --with-systemd \
  --with-libxml \
  --with-libxslt \
  --with-readline \
  --with-zlib \
  --with-pgport=5432

5、编译安装

make -j$(nproc)
make instal

6、创建用户并修改权限

mkdir /oms/data/pg17
useradd -m -s /bin/bash postgres
chown postgres:postgres -R /oms/postgres17.2/
chown postgres:postgres -R /oms/data/pg17

7、配置postgres用户环境变量(使用postgres用户操作)

su - postgres
cat >> ~/.profile<< EOF
export PGDATA=/data/pg17/
export LANG=en_US.utf8
export PGHOME=/Programs/postgres17.0/
export PATH=/Programs/postgres17.0/bin:\$PATH
export PGUSER=postgres
EOF
source ~/.profile

8、初始化数据库(使用postgres用户)

initdb -A md5 -D $PGDATA -E utf8 --locale=C -W

9、使用 systemd 管理 postgres

cat > /etc/systemd/system/postgres.service <<EOF
[Unit]
Description=PostgreSQL 17.0 database server
After=network.target

[Service]
Type=forking
User=postgres
Group=postgres

# Commands to manage the service
ExecStart=/oms/postgres17.2/bin/pg_ctl start -D /oms/data/pg17 -l \${PGDATA}/logfile
ExecStop=/oms/postgres17.2/bin/pg_ctl stop -D /oms/data/pg17 -s -m fast
ExecReload=/oms/postgres17.2/bin/pg_ctl reload -D /oms/data/pg17

# Restart settings
Restart=on-failure
RestartSec=5

# Limits
LimitNOFILE=65536
LimitNPROC=65536

[Install]
WantedBy=multi-user.target
EOF

10、修改 postgres 数据库配置文件(可选)

如果需要除安装 postgres 数据库的服务器以外的其他机器要连接使用数据库,需要进行以下操作,

# 修改pg_hba.conf文件,添加一下内容
host    all             all             0.0.0.0/0            md5

sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /data/pg17/postgresql.conf 
sed -i "s/#unix_socket_directories = '\/tmp'/unix_socket_directories = '\/tmp'/g" /data/pg17/postgresql.conf 

11 、设置 postgres 数据库开机启动

# 重载systemd
systemctl daemon-reload
# 设置设置 postgres  数据库开机自启并启动数据库服务
systemctl enable postgres --now

本文原始发布地址:http://www.omfox.cn

标签: DBA Linux PostgreSQL Rocky
最后更新:2025年5月15日

辰砂

这个人很懒,什么都没留下

点赞
< 上一篇

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复
搜索
标签聚合
MySQL Rocky Ubuntu Linux 数据库 PostgreSQL Postgres DBA
历史文章
  • 2025 年 5 月 / 2篇
  • 2025 年 4 月 / 2篇

COPYRIGHT © 2025 运维栈. ALL RIGHTS RESERVED.

Theme omfox Made By omfox

蜀ICP备2023010359号

川公网安备51010702042743号