大家好,我是 JiekeXu,江湖人称“强哥”,青学会MOP技术社区联合创始人,荣获 Oracle ACE Pro 称号,墨天轮 MVP,墨天轮年度“墨力之星”,拥有 Oracle OCP/OCM 认证,MySQL 5.7/8.0 OCP 认证以及 PCA、PCTA、OBCA、OGCA、KCP 等众多国产数据库认证证书,今天和大家一起来看看 Linux 8 快速安装 PostgreSQL 17.2,欢迎点击最上方蓝字“JiekeXu DBA之路”关注我的微信公众号,然后点击右上方三个点“设为星标”置顶,更多干货文章才能第一时间推送,谢谢!
前 言
一、查看操作系统信息
二、操作系统相关设置
三、下载软件包并解压
四、编译安装
编译安装要求
配置参数选项
配置
编译安装
编译中遇到的几个小错误
五、配置环境变量
六、初始化PG实例
七、启动PG数据库实例
八、登录测试
九、配置开机自启动
十、参考链接
前 言
2024 年 9 月 26 日 - PostgreSQL 全球开发组宣布 PostgreSQL 17 正式发布,作为世界上最先进的开源数据库,PostgreSQL 17 是目前的最新版本。2024 年 11 月 21 日:PostgreSQL 17.2、16.6、15.10、14.15、13.18 和 12.22 发布!五年一个大版本,PostgreSQL 的下一个主要版本计划是 PG18 版本,计划于 2025 年 9 月发布。
PostgreSQL 版本发布策略除另有说明外,这些发布的目标日期为每年 2 月、5 月、8 月和 11 月的第二个星期四,目前即将发布的时间表是
2025 年 2 月 13 日
2025 年 5 月 8 日
2025 年 8 月 14 日
2025 年 11 月 13 日
一、查看操作系统信息
[root@JiekeXu-Lix8 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.7 (Ootpa)
[root@JiekeXu-Lix8 ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.7"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.7"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.7"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:7:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.7
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.7
[root@JiekeXu-Lix8 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs 1.8G 1.4M 1.8G 1% /dev/shm
tmpfs 1.8G 34M 1.8G 2% /run
tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
/dev/mapper/ol_jiekexu--lix8-root 37G 6.7G 31G 19% /
/dev/mapper/ol_jiekexu--lix8-home 19G 453M 18G 3% /home
/dev/sda1 1014M 336M 679M 34% /boot
tmpfs 363M 32K 363M 1% /run/user/0
/dev/sr0 12G 12G 0 100% /run/media/root/OL-8-7-0-BaseOS-x86_64
[root@JiekeXu-Lix8 ~]# free -m
total used free shared buff/cache available
Mem: 3627 1475 113 136 2039 1724
Swap: 4051 0
二、操作系统相关设置
使用 root 用户操作,这里不在讲解具体细节,仅列出步骤即可。
1) 内核参数设置
cp /etc/sysctl.conf /etc/sysctl.conf_`date +"%Y%m%d_%H%M%S"`
cat >> /etc/sysctl.conf << "EOF"
############################for postgresql###########
kernel.shmall =4294967296
kernel.shmmax=135497418752
kernel.shmmni =4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max =7672460
fs.aio-max-nr =1048576
net.ipv4.ip_local_port_range= 9000 65000
net.core.rmem_default= 262144
net.core.rmem_max= 4194304
net.core.wmem_default= 262144
net.core.wmem_max= 4194304
net.ipv4.tcp_max_syn_backlog= 4096
net.core.netdev_max_backlog= 10000
net.ipv4.tcp_timestamps= 0
#net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_timestamps=1
net.ipv4.tcp_keepalive_time= 72
net.ipv4.tcp_keepalive_probes= 9
net.ipv4.tcp_keepalive_intvl= 7
vm.zone_reclaim_mode=0
vm.dirty_background_bytes= 40960000
vm.dirty_ratio =80
vm.dirty_expire_centisecs= 6000
vm.dirty_writeback_centisecs= 50
vm.swappiness=0
vm.overcommit_memory= 0
vm.overcommit_ratio= 90
EOF
使用 sysctl -p 生效
2)操作系统资源限制
cat >> /etc/security/limits.conf << "EOF"
#################for postgresql db ###########
postgres soft nofile 131072
postgres hard nofile 131072
postgres soft nproc 131072
postgres hard nproc 131072
postgres soft core unlimited
postgres hard core unlimited
postgres soft memlock 500000000
postgres hard memlock 500000000
EOF
3)关闭防火墙和 SeLinux
需要关闭 SELINUX 和操作系统防火墙 iptables
设置 selinux 为 disable 并重启操作系统;用 systemctl status firewalld 命令关闭防火墙。
cp /etc/selinux/config /etc/selinux/config_`date +"%Y%m%d_%H%M%S"`&& sed -i 's/SELINUX\=enforcing/SELINUX\=disabled/g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
4)配置网络 yum 源或者本地 yum 源
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
dnf clean all
dnf repolist
5) 安装编译所需 rpm 包
[root@JiekeXu-Lix8 ~]# yum install -y zlib zlib-devel libaio cmake make gcc gcc-c++ readline readline-devel perl \
bison flex libyaml net-tools expect openssh-clients tcl openssl openssl-devel libicu libicu-devel \
ncurses-devel python3 python3-devel openldap pam systemtap-sdt-devel perl-ExtUtils-Embed
[root@JiekeXu-Lix8 yum.repos.d]# yum install perl-Env bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel make libffi-devel -y
[root@JiekeXu-Lix8 ~]# rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" coreutils glib2 lrzsz mpstat dstatsysstat e4fsprogs xfsprogs ntp readline-devel zlib-devel openssl-develpam-devel libxml2-devel libxslt-devel python-devel tcl-devel gcc makesmartmontools flex bison perl-devel perl-Ext Utils* openldap-devel jadetex openjade bzip2 | grep 'not installed' |column -t
package mpstat is not installed
package dstatsysstat is not installed
package e4fsprogs is not installed
package ntp is not installed
package openssl-develpam-devel is not installed
package libxml2-devel is not installed
package libxslt-devel is not installed
package python-devel is not installed
package makesmartmontools is not installed
package perl-Ext is not installed
package Utils* is not installed
package openldap-devel is not installed
package jadetex is not installed
package openjade is not installed
6)创建用户密码
groupadd postgres
useradd -g postgres postgres
echo "postgres" |passwd --stdin postgres
密码:postgres
三、下载软件包并解压
[root@JiekeXu-Lix8 ~]# wget https://ftp.postgresql.org/pub/source/v17.2/postgresql-17.2.tar.gz
[root@JiekeXu-Lix8 ~]# tar -xvf /root/postgresql-17.2.tar.gz -C /home/postgres/
[root@JiekeXu-Lix8 ~]# cd /home/postgres/
[root@JiekeXu-Lix8 postgres]# ll
total 4
drwxrwxr-x 2 postgres postgres 6 Dec 11 23:44 pgdata
drwxrwxr-x 6 root root 4096 Nov 19 04:32 postgresql-17.2
[root@JiekeXu-Lix8 postgres]# chown -R postgres. postgresql-17.2/
[root@JiekeXu-Lix8 postgres]# ll
total 4
drwxrwxr-x 2 postgres postgres 6 Dec 11 23:44 pgdata
drwxrwxr-x 6 postgres postgres 4096 Nov 19 04:32 postgresql-17.2
[root@JiekeXu-Lix8 postgres]# su - postgres
[postgres@JiekeXu-Lix8 ~]$ ll
total 4
drwxrwxr-x 2 postgres postgres 6 Dec 11 23:44 pgdata
drwxrwxr-x 6 postgres postgres 4096 Nov 19 04:32 postgresql-17.2
[postgres@JiekeXu-Lix8 ~]$ ln -s postgresql-17.2 postgres
[postgres@JiekeXu-Lix8 ~]$ ll
total 4
drwxrwxr-x 2 postgres postgres 6 Dec 11 23:44 pgdata
lrwxrwxrwx 1 postgres postgres 16 Dec 11 23:47 postgres -> postgresql-17.2
drwxrwxr-x 6 postgres postgres 4096 Nov 19 04:32 postgresql-17.2
四、编译安装
编译安装要求
需要 GNU make 3.81 或更新版本;
需要 GCC 建议使用 GCC 的最新版本;
需要 gzip 或 bzip2 之外,还需要 tar 来解压源分发版;
需要 Flex 2.5.35 或更高版本以及 Bison 2.3 或更高版本;
需要 Perl 5.14 或更高版本;
默认情况下使用 GNU Readline 库。它允许psql 实现历史命令上下翻转;
默认需要 zlib 压缩库。如果不想使用它,则必须指定选项
--without-zlib
;使用此选项将禁用 pg_dump 和pg_restore 中对压缩存档的支持。默认情况下使用 ICU 库。如果您不想使用它,则必须指定选项
--without-icu
。使用此选项将禁用对 ICU 排序规则功能的支持。ICU 支持需要安装ICU4C包。目前ICU4C的最低要求版本为 4.2。要构建PL/Python服务器编程语言,您需要安装包含头文件和sysconfig模块的Python。最低要求版本为Python 3.2。
要构建PL/Tcl过程语言,您当然需要安装Tcl,最低要求版本是Tcl 8.4。
如果要支持加密的客户端连接,则需要 OpenSSL。在没有 /dev/urandom 的平台(Windows 除外)上,随机数生成也需要 OpenSSL。最低要求版本为 1.0.2。
如果您希望支持使用这些服务进行身份验证,则需要 MIT Kerberos(用于 GSSAPI)、OpenLDAP 和/或 PAM。
如果您希望支持使用该方法进行数据压缩,则需要 LZ4;请参阅 default_toast_compression 和 wal_compression。
如果您希望支持使用该方法进行数据压缩,则需要 Zstandard;请参阅 wal_compression。最低要求版本为 1.4.0。
要构建 PostgreSQL 文档,则需要:yum install docbook-dtds docbook-style-xsl libxslt fop
配置参数选项
使用 configure 命令便可以进行相关的配置操作,可以 -h 查看相关的帮助命令进行配置。
[postgres@JiekeXu-Lix8 postgres]$ ./configure -h
`configure' configures PostgreSQL 17.2 to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local/pgsql]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/pgsql/bin', `/usr/local/pgsql/lib' etc. You can specify
an installation prefix other than `/usr/local/pgsql' using `--prefix',
for instance `--prefix=$HOME'.
--几个选项的简单说明:
--with-pgport=NUMBER
设置NUMBER为服务器和客户端的默认端口号。默认值为 5432。以后可以随时更改端口,但如果您在此处指定端口,则服务器和客户端都将编译相同的默认值,这非常方便。通常,选择非默认值的唯一好理由是您打算在同一台机器上运行多个PostgreSQL服务器。
--enable-nls[=LANGUAGES]
启用本地语言支持 ( NLS ),即能够以英语以外的语言显示程序消息。LANGUAGES是一个可选的空格分隔的语言代码列表,其中包含您想要支持的语言,例如--enable-nls='de fr'。(您的列表和实际提供的翻译集之间的交集将自动计算。)如果您未指定列表,则将安装所有可用的翻译。
要使用此选项,您将需要实现Gettext API。
--with-perl
构建PL/Perl服务器端语言。
--with-python
构建PL/Python服务器端语言。
--with-tcl
构建PL/Tcl服务器端语言。
--with-lz4
使用LZ4压缩支持进行构建。
--with-zstd
使用Zstandard压缩支持进行构建。
--with-pam
使用PAM进行构建(可插入身份验证模块)支持。
--without-icu
无需支持ICU即可构建库,禁用 ICU 排序功能
--with-extra-version=STRING
附加STRING到 PostgreSQL 版本号。例如,您可以使用它来标记从未发布的 Git 快照构建的二进制文件或包含带有额外版本字符串(例如标识符git describe或分发包发布号)的自定义补丁。
配置
[root@JiekeXu-Lix8 yum.repos.d]# yum install make gcc gcc-c++ gzip bzip2-devel flex bison perl perl-Env perl-ExtUtils-Embed readline-devel zlib zlib-devel icu libicu-devel python3 python3-devel tcl openssl openssl-devel
[root@JiekeXu-Lix8 ~]# python3
Python 3.6.8 (default, Oct 5 2022, 16:22:51)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-15.0.1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[postgres@JiekeXu-Lix8 postgres]$ ./configure --prefix=/home/postgres/postgres --with-perl --with-python --with-pgport=54321 --with-openssl
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 54321
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for gcc option to accept ISO C99... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for gawk... gawk
checking whether gcc supports -Wdeclaration-after-statement, for CFLAGS... yes
checking whether gcc supports -Werror=vla, for CFLAGS... ye
…… --省略中间过程s
checking for __int128... yes
checking for __int128 alignment bug... ok
checking alignment of PG_INT128_TYPE... 16
checking for builtin __sync char locking functions... yes
checking for builtin __sync int32 locking functions... yes
checking for builtin __sync int32 atomic operations... yes
checking for builtin __sync int64 atomic operations... yes
checking for builtin __atomic int32 atomic operations... yes
checking for builtin __atomic int64 atomic operations... yes
checking for __get_cpuid... yes
checking for __get_cpuid_count... yes
checking for __cpuid... no
checking for __cpuidex... no
checking for _xgetbv with CFLAGS=... no
checking for _xgetbv with CFLAGS=-mxsave... no
checking for _mm512_popcnt_epi64 with CFLAGS=... no
checking for _mm512_popcnt_epi64 with CFLAGS=-mavx512vpopcntdq -mavx512bw... yes
checking for _mm_crc32_u8 and _mm_crc32_u32 with CFLAGS=... no
checking for _mm_crc32_u8 and _mm_crc32_u32 with CFLAGS=-msse4.2... yes
checking for __crc32cb, __crc32ch, __crc32cw, and __crc32cd with CFLAGS=... no
checking for __crc32cb, __crc32ch, __crc32cw, and __crc32cd with CFLAGS=-march=armv8-a+crc... no
checking for __builtin_loongarch_crcc_w_b_w, __builtin_loongarch_crcc_w_h_w, __builtin_loongarch_crcc_w_w_w and __builtin_loongarch_crcc_w_d_w... no
checking which CRC-32C implementation to use... SSE 4.2 with runtime check
checking for library containing sem_init... none required
checking which semaphore API to use... unnamed POSIX
checking which random number source to use... OpenSSL
checking for perl.h... yes
checking for libperl... yes
checking Python.h usability... yes
checking Python.h presence... yes
checking for Python.h... yes
checking for xmllint... /usr/bin/xmllint
checking for xsltproc... /usr/bin/xsltproc
checking for fop... no
checking for dbtoepub... no
checking whether gcc supports -Wl,--as-needed, for LDFLAGS... yes
checking whether gcc supports -Wl,--export-dynamic, for LDFLAGS_EX_BE... yes
configure: using compiler=gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)
configure: using CFLAGS=-Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2
configure: using CPPFLAGS= -D_GNU_SOURCE
configure: using LDFLAGS= -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
[postgres@JiekeXu-Lix8 postgres]$
编译安装
[postgres@JiekeXu-Lix8 postgres]$ gmake world -j2
gmake[4]: Nothing to be done for 'all'.
gmake[4]: Leaving directory '/home/postgres/postgresql-17.2/src/common'
gmake[3]: Leaving directory '/home/postgres/postgresql-17.2/src/test/regress'
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -I. -I. -I../../../src/interfaces/libpq -I./../regress -I../../../src/include -D_GNU_SOURCE -c -o specparse.o specparse.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -I. -I. -I../../../src/interfaces/libpq -I./../regress -I../../../src/include -D_GNU_SOURCE -c -o specscanner.o specscanner.c
rm -f pg_regress.o && ln -s ../../../src/test/regress/pg_regress.o .
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 isolation_main.o pg_regress.o -L../../../src/interfaces/libpq -lpq -L../../../src/port -L../../../src/common -Wl,--as-needed -Wl,-rpath,'/home/postgres/postgres/lib',--enable-new-dtags -lpgcommon -lpgport -lssl -lcrypto -lz -lreadline -lpthread -lrt -ldl -lm -o pg_isolation_regress
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 isolationtester.o specparse.o specscanner.o -L../../../src/interfaces/libpq -lpq -L../../../src/port -L../../../src/common -Wl,--as-needed -Wl,-rpath,'/home/postgres/postgres/lib',--enable-new-dtags -lpgcommon -lpgport -lssl -lcrypto -lz -lreadline -lpthread -lrt -ldl -lm -o isolationtester
gmake[2]: Leaving directory '/home/postgres/postgresql-17.2/src/test/isolation'
gmake -C test/perl all
gmake[2]: Entering directory '/home/postgres/postgresql-17.2/src/test/perl'
gmake[2]: Nothing to be done for 'all'.
gmake[2]: Leaving directory '/home/postgres/postgresql-17.2/src/test/perl'
gmake[1]: Leaving directory '/home/postgres/postgresql-17.2/src'
[postgres@JiekeXu-Lix8 postgres]$
[postgres@JiekeXu-Lix8 postgres]$ gmake install-world -j2
gmake[3]: Entering directory '/home/postgres/postgresql-17.2/src/common'
gmake[3]: Nothing to be done for 'all'.
gmake[3]: Leaving directory '/home/postgres/postgresql-17.2/src/common'
/usr/bin/mkdir -p '/home/postgres/postgres/lib/pgxs/src/test/isolation'
gmake[4]: Entering directory '/home/postgres/postgresql-17.2/src/common'
gmake[4]: Nothing to be done for 'all'.
gmake[4]: Leaving directory '/home/postgres/postgresql-17.2/src/common'
gmake[3]: Leaving directory '/home/postgres/postgresql-17.2/src/interfaces/libpq'
/usr/bin/install -c pg_isolation_regress '/home/postgres/postgres/lib/pgxs/src/test/isolation/pg_isolation_regress'
/usr/bin/install -c isolationtester '/home/postgres/postgres/lib/pgxs/src/test/isolation/isolationtester'
gmake[2]: Leaving directory '/home/postgres/postgresql-17.2/src/test/isolation'
gmake -C test/perl install
gmake[2]: Entering directory '/home/postgres/postgresql-17.2/src/test/perl'
gmake[2]: Nothing to be done for 'install'.
gmake[2]: Leaving directory '/home/postgres/postgresql-17.2/src/test/perl'
/usr/bin/mkdir -p '/home/postgres/postgres/lib/pgxs/src'
/usr/bin/install -c -m 644 Makefile.global '/home/postgres/postgres/lib/pgxs/src/Makefile.global'
/usr/bin/install -c -m 644 Makefile.port '/home/postgres/postgres/lib/pgxs/src/Makefile.port'
/usr/bin/install -c -m 644 ./Makefile.shlib '/home/postgres/postgres/lib/pgxs/src/Makefile.shlib'
/usr/bin/install -c -m 644 ./nls-global.mk '/home/postgres/postgres/lib/pgxs/src/nls-global.mk'
gmake[1]: Leaving directory '/home/postgres/postgresql-17.2/src'
[postgres@JiekeXu-Lix8 postgres]$
--查看版本
[postgres@JiekeXu-Lix8 postgres]$ /home/postgres/postgres/bin/postgres --version
postgres (PostgreSQL) 17.2
仅客户端安装
--仅客户端安装:如果您只想安装客户端应用程序和接口库,那么您可以使用以下命令:
make -C src/bin install
make -C src/include install
make -C src/interfaces install
make -C doc install
编译中遇到的几个小错误
错误一:缺少 ICU 包,error: ICU library not found
解决办法:安装 ICU 包或者编译时 --without-icu
[postgres@JiekeXu-Lix8 postgres]$ ./configure --prefix=/home/postgres/postgres --with-perl --with-python --with-pgport=54321 --with-openssl
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for gcc option to accept ISO C99... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking whether g++ supports -fvisibility=hidden, for CXXFLAGS_SL_MODULE... yes
checking whether g++ supports -fvisibility-inlines-hidden, for CXXFLAGS_SL_MODULE... yes
checking whether the C compiler still works... yes
checking how to run the C preprocessor... gcc -E
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking whether to build with ICU support... yes
checking for icu-uc icu-i18n... no
configure: error: ICU library not found
If you have ICU already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-icu to disable ICU support.
[root@JiekeXu-Lix8 ~]# icu-config --version
bash: icu-config: command not found...
Install package 'libicu-devel' to provide command 'icu-config'? [N/y] y
* Waiting in queue...
* Loading list of packages....
The following packages have to be installed:
libicu-devel-60.3-2.el8_1.x86_64 Development files for International Components for Unicode
Proceed with changes? [N/y] y
* Waiting in queue...
* Waiting for authentication...
* Waiting in queue...
* Loading list of packages....
* Downloading packages...
* Requesting data...
* Testing changes...
* Installing packages...
60.3
错误二:缺少 bison 包 error: bison not found
解决办法:安装 bison 包
checking for strip... strip
checking whether it is possible to strip libraries... yes
checking for ar... ar
checking for a BSD-compatible install... /usr/bin/install -c
checking for tar... /usr/bin/tar
checking whether ln -s works... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for bison... no
configure: error: bison not found
[root@JiekeXu-Lix8 ~]# yum install bison -y
错误三: 缺少 flex 包 error: flex not found
解决办法:安装 flex 包
checking whether to build with ZSTD support... no
checking for strip... strip
checking whether it is possible to strip libraries... yes
checking for ar... ar
checking for a BSD-compatible install... /usr/bin/install -c
checking for tar... /usr/bin/tar
checking whether ln -s works... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for bison... /usr/bin/bison
configure: using bison (GNU Bison) 3.0.4
checking for flex... no
configure: error: flex not found
[root@JiekeXu-Lix8 ~]# yum install flex -y
五、配置环境变量
vi .bashrc
export PGPORT=54321
export PGDATA=/home/postgres/pgdata
export PGHOME=/home/postgres/postgres
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
export LANG='en_US.UTF-8'
alias rm='rm -i'
alias ll='ls -lh'
六、初始化PG实例
initdb -D $PGDATA -E UTF8 --locale=C -U postgres
[postgres@JiekeXu-Lix8 ~]$ initdb -D $PGDATA -E UTF8 --locale=C -U postgres
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 "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /home/postgres/pgdata ... 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 /home/postgres/pgdata -l logfile start
七、启动PG数据库实例
首先编辑参数文件 postgresql.conf
[root@JiekeXu-Lix8 ~]# su - postgres
[postgres@JiekeXu-Lix8 ~]$ cd $PGDATA
vi postgresql.conf
listen_addresses = '0.0.0.0'
port = 54321
max_connections = 2000
unix_socket_directories = '/home/postgres/pgdata'
tcp_keepalives_idle = 60
tcp_keepalives_interval = 10
tcp_keepalives_count = 10
shared_buffers = 512MB
dynamic_shared_memory_type = posix
vacuum_cost_delay = 0
bgwriter_delay = 10ms
bgwriter_lru_maxpages = 1000
bgwriter_lru_multiplier = 10.0
bgwriter_flush_after = 0
backend_flush_after = 0
wal_level=logical
archive_mode=always
archive_command='cp %p /home/postgres/pgdata/archive/%f'
min_wal_size=128MB
max_wal_size=1GB
max_wal_senders=10
hot_standby=on
log_filename='pg_log_%u.log'
log_file_mode=0600
log_truncate_on_rotation=on
log_rotation_age=1d
log_min_messages=warning
log_min_duration_statement=30s
synchronous_commit = off
full_page_writes = on
wal_buffers = 16MB
wal_writer_delay = 10ms
wal_writer_flush_after = 0
checkpoint_timeout = 30min
checkpoint_completion_target = 0.05
checkpoint_flush_after = 0
random_page_cost = 1.3
log_directory='pg_log'
log_destination = 'csvlog'
logging_collector = on
log_truncate_on_rotation = on
log_checkpoints = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose
log_duration=on
log_lock_waits=on
log_statement='mod'
autovacuum = on
log_autovacuum_min_duration = 0
autovacuum_naptime = 20s
autovacuum_vacuum_scale_factor = 0.05
autovacuum_freeze_max_age = 1500000000
autovacuum_multixact_freeze_max_age = 1600000000
autovacuum_vacuum_cost_delay = 0
vacuum_freeze_table_age = 1400000000
vacuum_multixact_freeze_table_age = 1500000000
datestyle = 'iso, mdy'
timezone = 'PRC'
lc_messages = 'C'
lc_monetary = 'C'
lc_numeric = 'C'
lc_time = 'C'
default_text_search_config = 'pg_catalog.english'
然后配置 pg_hba.conf,数据库防火墙文件名字 pg_hba.conf,将以下内容追加到文件末尾,表示允许网络用户使用用户密码连接到你的 postgresql 数据库.
[postgres@JiekeXu-Lix8 pgdata]$ vim /home/postgres/pgdata/pg_hba.conf
host all all 0.0.0.0/0 md5
启动数据库
[postgres@JiekeXu-Lix8 pgdata]$ pg_ctl -D /home/postgres/pgdata start &
[1] 91484
[postgres@JiekeXu-Lix8 pgdata]$ waiting for server to start....2024-12-12 00:18:57.221 CST [91486] LOG: 00000: redirecting log output to logging collector process
2024-12-12 00:18:57.221 CST [91486] HINT: Future log output will appear in directory "pg_log".
2024-12-12 00:18:57.221 CST [91486] LOCATION: SysLogger_Start, syslogger.c:733
done
server started
[1]+ Done pg_ctl -D /home/postgres/pgdata start
[postgres@JiekeXu-Lix8 pgdata]$
### 查看状态
[postgres@JiekeXu-Lix8 pgdata]$ ps -ef | grep postgres
root 75920 3774 0 Dec11 pts/1 00:00:00 su - postgres
postgres 75921 75920 0 Dec11 pts/1 00:00:00 -bash
root 91346 3834 0 00:13 pts/2 00:00:00 su - postgres
postgres 91347 91346 0 00:13 pts/2 00:00:00 -bash
postgres 91486 1 1 00:18 ? 00:00:00 /home/postgres/postgresql-17.2/bin/postgres -D /home/postgres/pgdata
postgres 91487 91486 0 00:18 ? 00:00:00 postgres: logger
postgres 91488 91486 0 00:18 ? 00:00:00 postgres: checkpointer
postgres 91489 91486 0 00:18 ? 00:00:00 postgres: background writer
postgres 91491 91486 0 00:18 ? 00:00:00 postgres: walwriter
postgres 91492 91486 0 00:18 ? 00:00:00 postgres: autovacuum launcher
postgres 91493 91486 0 00:18 ? 00:00:00 postgres: archiver
postgres 91494 91486 0 00:18 ? 00:00:00 postgres: logical replication launcher
postgres 91506 75921 0 00:19 pts/1 00:00:00 ps -ef
postgres 91507 75921 0 00:19 pts/1 00:00:00 grep --color=auto postgres
[postgres@JiekeXu-Lix8 pgdata]$ pg_ctl status
pg_ctl: server is running (PID: 91486)
/home/postgres/postgresql-17.2/bin/postgres "-D" "/home/postgres/pgdata"
八、登录测试
--查看并启动 PG 实例
[postgres@JiekeXu-Lix8 ~]$ pg_ctl status
pg_ctl: no server running
[postgres@JiekeXu-Lix8 ~]$ pg_ctl start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2024-12-17 23:43:51.023 CST [29226] LOG: 00000: redirecting log output to logging collector process
2024-12-17 23:43:51.023 CST [29226] HINT: Future log output will appear in directory "pg_log".
2024-12-17 23:43:51.023 CST [29226] LOCATION: SysLogger_Start, syslogger.c:733
done
server started
[postgres@JiekeXu-Lix8 ~]$ pg_ctl status
pg_ctl: server is running (PID: 29226)
/home/postgres/postgresql-17.2/bin/postgres
[postgres@JiekeXu-Lix8 pgdata]$ psql -U postgres -p54321
psql (17.2)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+---------+-------+--------+-----------+-----------------------
postgres | postgres | UTF8 | libc | C | C | | |
template0 | postgres | UTF8 | libc | C | C | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | C | C | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(3 rows)
postgres=# create database jiekexu;
CREATE DATABASE
postgres=# \l
postgres=# \c jiekexu
You are now connected to database "jiekexu" as user "postgres".
jiekexu=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+---------+-------+--------+-----------+-----------------------
jiekexu | postgres | UTF8 | libc | C | C | | |
postgres | postgres | UTF8 | libc | C | C | | |
template0 | postgres | UTF8 | libc | C | C | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | C | C | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(4 rows)
jiekexu=# create table test (id int,name varchar(30));
CREATE TABLE
jiekexu=#
jiekexu=# insert into test values(1,'jieke'),(2,'DBA');
INSERT 0 2
jiekexu=# select * from test;
id | name
----+-------
1 | jieke
2 | DBA
(2 rows)
九、配置开机自启动
以前都是通过 contrib 目录下的 Linux 脚本设置开机自启动,也是很方便,仅需要修改 prefix 和 PGDATA 两处即可,今天我们通过配置成服务名的方式来设置开机自启动。
# cp /home/postgres/postgres/contrib/start-scripts/linux /etc/init.d/postgres-14
# chmod +x /etc/init.d/postgres-14
# vi /etc/init.d/postgres-14
--修改如下两处
prefix=/home/postgres/postgres
PGDATA="/home/postgres/pgdata"
# chkconfig postgres-14 on
# chkconfig --list | grep postgres
使用 root 用户,先关闭数据库实例,在进行配置
[root@JiekeXu-Lix8 ~]# su - postgres
[postgres@JiekeXu-Lix8 ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@JiekeXu-Lix8 ~]$ exit
logout
[root@JiekeXu-Lix8 ~]# cat > /usr/lib/systemd/system/postgres.service << "EOF"
cat > /usr/lib/systemd/system/postgres.service << "EOF"
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
Environment=PGPORT=54321
Environment=PGDATA=/home/postgres/pgdata
OOMScoreAdjust=-1000
ExecStart=/home/postgres/postgres/bin/pg_ctl start -D $PGDATA
ExecStop=/home/postgres/postgres/bin/pg_ctl stop -D $PGDATA -s -m fast
ExecReload=/home/postgres/postgres/bin/pg_ctl reload -D $PGDATA -s
TimeoutSec=300
[Install]
WantedBy=multi-user.target
EOF
[root@JiekeXu-Lix8 ~]# chmod +x /usr/lib/systemd/system/postgres.service
[root@JiekeXu-Lix8 ~]# systemctl daemon-reload
[root@JiekeXu-Lix8 ~]# systemctl enable --now postgres.service
Created symlink from /etc/systemd/system/multi-user.target.wants/postgres.service to /usr/lib/systemd/system/postgres.service.
[root@JiekeXu-Lix8 ~]# systemctl status postgres.service
[root@JiekeXu-Lix8 ~]# systemctl start postgres.service
[root@JiekeXu-Lix8 ~]# systemctl status postgres.service
● postgres.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgres.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2024-12-18 00:05:22 CST; 4s ago
Process: 29722 ExecStart=/home/postgres/postgres/bin/pg_ctl start -D $PGDATA (code=exited, status=0/SUCCESS)
Main PID: 29724 (postgres)
Tasks: 8 (limit: 22838)
Memory: 96.8M
CGroup: /system.slice/postgres.service
├─29724 /home/postgres/postgresql-17.2/bin/postgres -D /home/postgres/pgdata
├─29725 postgres: logger
├─29726 postgres: checkpointer
├─29727 postgres: background writer
├─29729 postgres: walwriter
├─29730 postgres: autovacuum launcher
├─29731 postgres: archiver
└─29732 postgres: logical replication launcher
Dec 18 00:05:22 JiekeXu-Lix8 systemd[1]: Starting PostgreSQL database server...
Dec 18 00:05:22 JiekeXu-Lix8 pg_ctl[29722]: waiting for server to start....
Dec 18 00:05:22 JiekeXu-Lix8 pg_ctl[29724]: waiting for server to start....
Dec 18 00:05:22 JiekeXu-Lix8 pg_ctl[29724]: 024-12-18 00:05:22.931 CST [29724] LOG: 00000: redirecting log output to>
Dec 18 00:05:22 JiekeXu-Lix8 pg_ctl[29724]: 2024-12-18 00:05:22.931 CST [29724] HINT: Future log output will appear >
Dec 18 00:05:22 JiekeXu-Lix8 pg_ctl[29724]: 2024-12-18 00:05:22.931 CST [29724] LOCATION: Sy
Dec 18 00:05:22 JiekeXu-Lix8 systemd[1]: Started PostgreSQL database server.
十、参考链接
https://www.modb.pro/db/1783744765954707456
https://www.postgresql.org/support/versioning/
https://en.wikipedia.org/wiki/PostgreSQL
https://www.postgresql.org/support/security/
https://www.postgresql.org/ftp/source/v17.2/
全文完,希望可以帮到正在阅读的你,如果觉得有帮助,可以分享给你身边的朋友,同事,你关心谁就分享给谁,一起学习共同进步~~~
欢迎关注我的公众号【JiekeXu DBA之路】,一起学习新知识!
—————————————————————
公众号:JiekeXu DBA之路
墨天轮:https://www.modb.pro/u/4347
CSDN :https://blog.csdn.net/JiekeXu
ITPUB:https://blog.itpub.net/69968215
腾讯云:https://cloud.tencent.com/developer/user/5645107
—————————————————————
分享几个数据库备份脚本
一文搞懂 Oracle 统计信息
我的 Oracle ACE 心路历程
MOP 系列|MOP 三种主流数据库索引简介
Oracle 主流版本不同架构下的静默安装指南
关机重启导致 ASM 磁盘丢失数据库无法启动
Oracle SQL 性能分析(SPA)原理与实战演练
Oracle 11g 升级到 19c 需要关注的几个问题
Windows 10 环境下 MySQL 8.0.33 安装指南
SQL 大全(四)|数据库迁移升级时常用 SQL 语句
OGG|使用 OGG19c 迁移 Oracle11g 到 19C(第二版)
Oracle 大数据量导出工具——sqluldr2 的安装与使用
Oracle ACE 视角下的国产数据库现状与选型及应对策略
从国产数据库调研报告中你都能了解哪些信息及我的总结建议
使用数据泵利用 rowid 分片导出导入 lob 大表及最佳实践
在归档模式下直接 rm dbf 数据文件并重启数据库还有救吗?