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

相关推荐
qq_5298353517 分钟前
Redis作为缓存和数据库的数据一致性问题
数据库·redis·缓存
山猪打不过家猪5 小时前
ASP.NET Core Clean Architecture
java·数据库·asp.net
qwy7152292581636 小时前
13-R数据重塑
服务器·数据库·r语言
Bio Coder6 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言
钊兵7 小时前
数据库驱动免费下载(Oracle、Mysql、达梦、Postgresql)
数据库·mysql·postgresql·oracle·达梦·驱动
weixin_425878238 小时前
Redis复制性能优化利器:深入解析replica-lazy-flush参数
数据库·redis·性能优化
左灯右行的爱情8 小时前
Redis数据结构总结-listPack
数据结构·数据库·redis
隔壁老王1569 小时前
mysql实时同步到es
数据库·mysql·elasticsearch
想要打 Acm 的小周同学呀9 小时前
Redis三剑客解决方案
数据库·redis·缓存