PostgreSQL跨版本升级

223 阅读3分钟
作者:IT邦德
中国DBA联盟(ACDU)成员,10余年DBA工作经验
擅长主流数据Oracle、MySQL、PG、openGauss运维
备份恢复,安装迁移,性能优化、故障应急处理等

可提供技术业务:
1.DB故障处理/疑难杂症远程支援
2.Mysql/PG/Oracle/openGauss
数据库部署及数仓搭建

@

前言

一种更快的方式pg_upgrade可以实现PostgreSQL版本升级,本文详细阐述了PG9升级到PG14


📣 1.PG升级介绍

数据库升级分为两种,一种是小版本迭代升级,另一种是主板本升级。
小版本升级很简单,只需要重启一下数据库即可。要在兼容的版本间升级,
你只需要简单地在服务器关闭时替换可执行文件并重启服务器。 
数据目录则保持不变--次要升级就这么简单。
对于PostgreSQL的主发行,
内部数据存储格式常被改变,这使升级复杂化。
传统的把数据移动到新主版本的方法是先转储然后重新载入到数据库,
不过这可能会很慢,所以使用 一种更快的方式pg_upgrade。

📣 2.安装PG14.9

✨ 1.1 创建用户

groupadd -g 60000 pg14
useradd -u 60000 -g pg14 pg14
echo "pg14" | passwd --stdin pg14

✨ 1.2 创建目录

mkdir -p /pg96/{pgdata,archive,scripts,backup,postgresql,soft}
cp /opt/postgresql-9.6.24.tar.gz /pg96/soft/
chown -R pg96:pg96 /pg96
chmod -R 775 /pg96

✨ 1.3 安装依赖

yum install -y cmake make gcc zlib gcc-c++ perl readline readline-devel
yum install -y zlib-devel perl python36 tcl openssl ncurses-devel openldap pam
yum install -y zlib libicu

✨ 1.4 编译

su - pg14
cd /pg14/soft
tar zxvf postgresql-14.9.tar.gz
cd postgresql-14.9
./configure --prefix=/pg14/postgresql --without-readline
make -j 4 && make install

📣 3. 9.6.24升级到14.9

✨ 3.1 设置权限

chown -R pg96:pg96 /pg14

✨ 3.2 新版本初始化

su - pg96
rm -rf /pg14/pgdata
/pg14/postgresql/bin/initdb -D /pg14/pgdata -E UTF8 --locale=en_US.utf8 -U postgres

✨ 3.3 检查兼容性

/pg14/postgresql/bin/pg_upgrade -c -k -b /pg96/postgresql/bin -B /pg14/postgresql/bin -d /pg96/pgdata -D /pg14/pgdata -p 5400 -P 5432

✨ 3.4 创建测试数据

postgres=# create database testdb;
[pg96@centos79 ~]pgbench -i -s 10 -U postgres testdb

✨ 3.5 执行升级

升级前需要关闭源库

[pg96@centos79 ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped

[pg96@centos79 ~]$ /pg14/postgresql/bin/pg_upgrade -k -b /pg96/postgresql/bin -B /pg14/postgresql/bin -d /pg96/pgdata -D /pg14/pgdata -p 5400 -P 5432

✨ 3.6 修改配置文件

新版本的postgresql.conf和pg_hba.conf等配置文件匹配旧集簇参数
cat >> /pg14/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/pg14/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF

cat > /pg14/pgdata/pg_hba.conf << EOF
# TYPE  DATABASE    USER    ADDRESS       METHOD
local     all       all                    trust
host      all       all   127.0.0.1/32     trust
host      all       all    0.0.0.0/0        md5
host   replication  all    0.0.0.0/0        md5
EOF

✨ 3.7 修改环境变量

cat >> ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]$ "
export PGPORT=5432
export PGDATA=/pg14/pgdata
export PGHOME=/pg14/postgresql
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
EOF

source  ~/.bash_profile

✨ 3.8 收集统计信息

[pg96@centos79 ~]$ pg_ctl start
[pg96@centos79 ~]$ /pg14/postgresql/bin/vacuumdb --all --analyze-in-stages

✨ 3.9 校验数据

[pg96@centos79 ~]$ psql
psql (14.9)
Type "help" for help.

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | postgres=CTc/postgres+
           |          |          |            |            | =c/postgres
 testdb    | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
(4 rows)

postgres=# \c testdb postgres
You are now connected to database "testdb" as user "postgres".
testdb=# \dt
              List of relations
 Schema |       Name       | Type  |  Owner
--------+------------------+-------+----------
 public | pgbench_accounts | table | postgres
 public | pgbench_branches | table | postgres
 public | pgbench_history  | table | postgres
 public | pgbench_tellers  | table | postgres
(4 rows)

testdb=select version();
                                                 version
---------------------------------------------------------------------------------------------------------
 PostgreSQL 14.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)

相关推荐阅读:
90天精通Oracle-实战系列
Oracle高可用实战系列
MySQL从入门到实战
如何成为SQL高手
PostgreSQL从入门到实战

大家可以点赞、收藏、关注、评论我啦 、有数据库相关的问题随时联系我或交流哟~!

image.png