Ubuntu24.04源码安装postgresql17

一、什么是 Postgresql

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

二、环境

  • 系统: Ubuntu24.04 LTS sever
  • 数据库: Postgresql 17.0

三、安装步骤

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

1、安装依赖环境

bash 复制代码
apt-get install -y systemtap-sdt-dev libicu-dev libreadline-dev zlib1g-dev libssl-dev libpam0g-dev libxml2-dev libxslt1-dev  libldap-dev libsystemd-dev  gettext tcl-dev libpython3-dev libperl-dev

2、调整内核系统参数

bash 复制代码
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、下载并解压源码文件

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

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

tar -xf postgresql-17.0.tar.gz
cd postgresql-17.0

4、配置编译参数

bash 复制代码
./configure \
  --prefix=/Programs/postgres17.0 \
  --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、编译安装

bash 复制代码
make -j$(nproc)
make instal

6、创建用户并修改权限

bash 复制代码
mkdir /data/pg17
useradd -m -s /bin/bash postgres
chown postgres:postgres -R /Programs/postgres17.0/
chown postgres:postgres -R /data/pg17

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

shell 复制代码
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用户)

bash 复制代码
initdb -A md5 -D $PGDATA -E utf8 --locale=C -W

9、使用 systemd 管理 postgres

bash 复制代码
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=/Programs/postgres17.0/bin/pg_ctl start -D /data/pg17 -l /data/pg17/logfile
ExecStop=/Programs/postgres17.0/bin/pg_ctl stop -D /data/pg17 -s -m fast
ExecReload=/Programs/postgres17.0/bin/pg_ctl reload -D /data/pg17

# Restart settings
Restart=on-failure
RestartSec=5

# Limits
LimitNOFILE=65536
LimitNPROC=65536

[Install]
WantedBy=multi-user.target
EOF

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

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

bash 复制代码
# 修改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 数据库开机启动

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

文章原始发布地址:www.omfox.cn

相关推荐
怕什么真理无穷4 分钟前
mysql server 9.4 windows安装教程(sqlyog 下载)
数据库
Olrookie10 分钟前
MySQL运维常用SQL
运维·数据库·sql·mysql·dba
数据库生产实战20 分钟前
ORACLE 19C ADG环境 如何快速删除1.8TB的分区表?有哪些注意事项?
数据库·oracle
blackorbird37 分钟前
使用 Overpass Turbo 查找监控摄像头
运维·服务器·数据库·windows
IT永勇41 分钟前
SQLite数据库基本操作
数据库·sqlite·嵌入式开发·增删改查·关系型数据库
洋不写bug43 分钟前
数据库的创建,查看,修改,删除,字符集编码和校验操作
android·数据库·adb
想ai抽1 小时前
吃透大数据算法-算法地图(备用)
大数据·数据库·spark
weixin_307779131 小时前
Clickhouse导出库的表、视图、用户和角色定义的SQL语句
开发语言·数据库·算法·clickhouse·自动化
流星白龙1 小时前
【Qt】7.信号和槽_connect函数用法(1)
开发语言·数据库·qt
码界奇点1 小时前
平替MongoDB金仓多模数据库在电子证照国产化中的实践与优势
数据库·mongodb·社交电子·里氏替代原则