PostgreSQL 软件升级
1 源码小版本(17.1升级到17.5)
只需要关闭数据库,更新bin文件,然后启动数据库即可
postgres@u24-pg-110:/postgresql/data$ ls -l /usr/local/postgresql-17/bin/
total 17536
-rwxr-xr-x 1 root root 123408 May 23 19:37 clusterdb
-rwxr-xr-x 1 root root 123168 May 23 19:37 createdb
-rwxr-xr-x 1 root root 123992 May 23 19:37 createuser
-rwxr-xr-x 1 root root 118584 May 23 19:37 dropdb
-rwxr-xr-x 1 root root 118512 May 23 19:37 dropuser
-rwxr-xr-x 1 root root 1065984 May 23 19:37 ecpg
-rwxr-xr-x 1 root root 218200 May 23 19:37 initdb
-rwxr-xr-x 1 root root 154560 May 23 19:37 pg_amcheck
-rwxr-xr-x 1 root root 56824 May 23 19:37 pg_archivecleanup
-rwxr-xr-x 1 root root 226720 May 23 19:37 pg_basebackup
-rwxr-xr-x 1 root root 254256 May 23 19:37 pgbench
-rwxr-xr-x 1 root root 96600 May 23 19:37 pg_checksums
-rwxr-xr-x 1 root root 184256 May 23 19:37 pg_combinebackup
-rwxr-xr-x 1 root root 59264 May 23 19:37 pg_config
-rwxr-xr-x 1 root root 73776 May 23 19:37 pg_controldata
-rwxr-xr-x 1 root root 149392 May 23 19:37 pg_createsubscriber
-rwxr-xr-x 1 root root 88968 May 23 19:37 pg_ctl
-rwxr-xr-x 1 root root 517976 May 23 19:37 pg_dump
-rwxr-xr-x 1 root root 171664 May 23 19:37 pg_dumpall
-rwxr-xr-x 1 root root 113960 May 23 19:37 pg_isready
-rwxr-xr-x 1 root root 132088 May 23 19:37 pg_receivewal
-rwxr-xr-x 1 root root 127592 May 23 19:37 pg_recvlogical
-rwxr-xr-x 1 root root 88256 May 23 19:37 pg_resetwal
-rwxr-xr-x 1 root root 284120 May 23 19:37 pg_restore
-rwxr-xr-x 1 root root 212816 May 23 19:37 pg_rewind
-rwxr-xr-x 1 root root 62264 May 23 19:37 pg_test_fsync
-rwxr-xr-x 1 root root 55504 May 23 19:37 pg_test_timing
-rwxr-xr-x 1 root root 232472 May 23 19:37 pg_upgrade
-rwxr-xr-x 1 root root 140408 May 23 19:37 pg_verifybackup
-rwxr-xr-x 1 root root 138904 May 23 19:37 pg_waldump
-rwxr-xr-x 1 root root 83768 May 23 19:37 pg_walsummary
-rwxr-xr-x 1 root root 11109520 May 23 19:37 postgres
-rwxr-xr-x 1 root root 906296 May 23 19:37 psql
-rwxr-xr-x 1 root root 132552 May 23 19:37 reindexdb
-rwxr-xr-x 1 root root 137160 May 23 19:37 vacuumdb
2 大版本升级(15.7升级到17.5)
需要检查旧版本已经安装的外部扩展,有一些外部扩展要求在升级之前先升级旧版本的外部扩展,例如PostGIS。
pg_upgrade 参数选项
-b --old-bindir=BINDIR 旧版本PostgreSQL 的可执行文件目录;环境变量名称为 PGBINOLD
-B --new-bindir=BINDIR 新版本PostgreSQL 的可执行文件目录;默认路径为 pg_upgrade 所在目录;环境变量名称为 PGBINNEW
-c --check 只检查集群升级兼容性,不会真正的升级,不改变数据
-d --old-datadir=configdir 旧版本数据库配置/数据目录;环境变 量名称为 PGDATAOLD
-D --new-datadir=configdir 新版本数据库配置/数据目录;环境变量名称为 PGDATANEW
-j --jobs 允许多个CPU 核复制或链接文件以及并行地转储和重载数据库模式,一般可以设置为 CPU 核数。这个选项可以显著地减少升级时间。
-k --link 使用硬链接方式而不是将文件copy 到新版本数据库的方式升级
-o --old-options=OPTIONS 直接传送给旧 postgres 命令的选项,多个选项可以追加在后面
-O--new-options=OPTIONS 直接传送给新 postgres 命令的选项,多个选项可以追加在后面
-p --old-port=PORT 旧版本数据库使用的端口号;环境变量名称为 PGPORTOLD
-P --new-port=PORT 新版本数据库使用的端口号;环境变量名称为PGPORTNEW;新旧版本实例使用的端口号必须不同
-r --retain 即使在成功完成后也保留SQL 和日志文件
-s --socketdir=DIR 在升级过程中 postmaster sockets 使用的目录,默认是当前工作目录,环境变量名称为PGSOCKETDIR
-U --username=username 数据库的安装用户;环境变量名称为PGUSER
-v --verbose 启用详细的内部日志记录
-V --version 显示版本信息,然后退出
–clone
#升级步骤
01.升级环境准备
原15.7的目录:
/usr/local/postgresql
/postgresql/data
/usr/local/postgresql-17
/postgresql/data17
数据准备:
先安装PG15.7,安装部分插件(pg_bulkload pg_recovery),导入部分数据
02.原数据库备份
1)物理备份
cp -r /postgresql/app/postgresql /postgresql/app/postgresql15
2)如果数据量少,建议把目录也复制一份。
cd /postgresql/
cp -r data data15
mkdir data16
postgresql.conf
pg_hba.conf
pg_ident.conf
postgresql.auto.conf
03.停止pg15.4的数据库服务
pg_ctl stop
04.安装新版本17.5(不初始化data目录)
参考源码安装文章
05.进行升级兼容性测试
/usr/local/postgresql17/bin/pg_upgrade -b /postgresql/app/postgresql/bin -B /postgresql/app/postgresql17/bin -d /postgresql/data/ -D /postgresql/data17 -c
06.升级15.7至17.5 升级命令
/usr/local/postgresql17/bin/pg_upgrade -b /postgresql/app/postgresql/bin -B /postgresql/app/postgresql17/bin -d /postgresql/data/ -D /postgresql/data17
07.将pg15的相关配置文件移动到pg17的data目录中,启动数据库
cd /postgresql/data
cp postgresql.conf /postgresql/data17/
cp pg_hba.conf /postgresql/data17/
cp postgresql.auto.conf /postgresql/data17/postgresql.auto.conf
08.根据升级完成后的提示,验证无误后执行删除旧数据目录脚本,清理空间