PostgreSQL版本通过pg_upgrade升级(9.6.21升级到13.2)

PG升级前版本:9.6.21

PG升级后版本:13.2

docker:18.06.3-ce

1、先下载相应的镜像,里面已经安装了PG各个版本:9.6、10、11、12、13

[root@wcbpg ~]# docker pull lhrbest/lhrpgall:1.0

创建镜像

[root@wcbpg ~]#docker run -d --name mambapg -h mambapg \-p 15432-15439:5432-5439 \-v /sys/fs/cgroup:/sys/fs/cgroup \--privileged=true registry.cn-hangzhou.aliyuncs.com/lhrbest/lhrpgall:1.0 \/usr/sbin/init
[root@wcbpg ~]# docker exec -it mambapg bash
#设置权限
[root@mambapg /]# chown -R pg96:pg96 /pg13

切换到pg96账号,这个账号里面配置的就是对应的pg9.6版本

[root@mambapg /]# su - pg96
Last login: Sat Dec 18 13:19:22 CST 2021 on pts/0
[pg96@mambapg ~]$ rm -rf /pg13/pgdata

初始化数据库

[pg96@mambapg ~]$ /pg13/pg13/bin/initdb -D /pg13/pgdata -E UTF8 --locale=en_US.utf8 -U postgres
The files belonging to this database system will be owned by user "pg96".
This user must also own the server process.The database cluster will be initialized with locale "en_US.utf8".
The default text search configuration will be set to "english".Data page checksums are disabled.creating directory /pg13/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 ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connections
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:/pg13/pg13/bin/pg_ctl -D /pg13/pgdata -l logfile start

2、检查兼容性

[pg96@mambapg ~]$ /pg13/pg13/bin/pg_upgrade -c -k -b /pg96/pg96/bin -B /pg13/pg13/bin -d /pg96/pgdata -D /pg13/pgdata -p 5436 -P 5432
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for invalid "unknown" user columns                 ok
Checking for hash indexes                                   ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok*Clusters are compatible*

3、源库创建测试数据

[pg96@mambapg ~]$ pg_ctl start
[pg96@mambapg ~]$ psql
psql (9.6.21)
Type "help" for help.postgres=# create database mambadb;
CREATE DATABASE
postgres=# \q
[pg96@mambapg ~]$ pgbench -i -s 10 -U postgres mambadb;
NOTICE:  table "pgbench_history" does not exist, skipping
NOTICE:  table "pgbench_tellers" does not exist, skipping
NOTICE:  table "pgbench_accounts" does not exist, skipping
NOTICE:  table "pgbench_branches" does not exist, skipping
creating tables...
100000 of 1000000 tuples (10%) done (elapsed 0.06 s, remaining 0.54 s)
200000 of 1000000 tuples (20%) done (elapsed 0.13 s, remaining 0.52 s)
300000 of 1000000 tuples (30%) done (elapsed 0.21 s, remaining 0.50 s)
400000 of 1000000 tuples (40%) done (elapsed 0.30 s, remaining 0.45 s)
500000 of 1000000 tuples (50%) done (elapsed 0.39 s, remaining 0.39 s)
600000 of 1000000 tuples (60%) done (elapsed 0.48 s, remaining 0.32 s)
700000 of 1000000 tuples (70%) done (elapsed 0.57 s, remaining 0.24 s)
800000 of 1000000 tuples (80%) done (elapsed 0.66 s, remaining 0.17 s)
900000 of 1000000 tuples (90%) done (elapsed 0.75 s, remaining 0.08 s)
1000000 of 1000000 tuples (100%) done (elapsed 0.84 s, remaining 0.00 s)
vacuum...
set primary keys...
done.
[pg96@mambapg ~]$ psql
psql (9.6.21)
Type "help" for help.postgres=# \c mambadb
You are now connected to database "mambadb" as user "postgres".
mambadb=# \dList of relationsSchema |       Name       | Type  |  Owner
--------+------------------+-------+----------public | pgbench_accounts | table | postgrespublic | pgbench_branches | table | postgrespublic | pgbench_history  | table | postgrespublic | pgbench_tellers  | table | postgres
(4 rows)mambadb=# select count(*) from pgbench_accounts;count
---------1000000
(1 row)

4、执行升级

数据库升级前需要先停库。在进行升级

[pg96@mambapg ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[pg96@mambapg ~]$ /pg13/pg13/bin/pg_upgrade  -k -b /pg96/pg96/bin -B /pg13/pg13/bin -d /pg96/pgdata -D /pg13/pgdata -p 5436 -P 5432
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for invalid "unknown" user columns                 ok
Creating dump of global objects                             ok
Creating dump of database schemasok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             okIf pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_clog to new server                           ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new clusterok
Adding ".old" suffix to old global/pg_control               okIf you want to start the old cluster, you will need to remove
the ".old" suffix from /pg96/pgdata/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.Linking user relation filesok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok
Checking for hash indexes                                   okUpgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:./analyze_new_cluster.shRunning this script will delete the old cluster's data files:./delete_old_cluster.sh
[pg96@mambapg ~]$ more ./analyze_new_cluster.sh
#!/bin/shecho 'This script will generate minimal optimizer statistics rapidly'
echo 'so your system is usable, and then gather statistics twice more'
echo 'with increasing accuracy.  When it is done, your system will'
echo 'have the default level of optimizer statistics.'
echoecho 'If you have used ALTER TABLE to modify the statistics target for'
echo 'any tables, you might want to remove them and restore them after'
echo 'running this script because they will delay fast statistics generation.'
echoecho 'If you would like default statistics as quickly as possible, cancel'
echo 'this script and run:'
echo '    "/pg13/pg13/bin/vacuumdb" --all --analyze-only'
echo"/pg13/pg13/bin/vacuumdb" --all --analyze-in-stages
echoecho 'Done'
[pg96@mambapg ~]$ more  ./delete_old_cluster.sh
#!/bin/shrm -rf '/pg96/pgdata'

5、恢复配置文件

新版本的postgresql.conf和pg_hba.conf等配置文件匹配旧集群簇参数。

[pg96@mambapg ~]$cat >> /pg13/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5436
unix_socket_directories='/pg13/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF
[pg96@mambapg ~]$ cat   > /pg13/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

6、修改环境变量

cat >>  ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]\$ "
export PGPORT=5436
export PGDATA=/pg13/pgdata
export PGHOME=/pg13/pg13
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=postgresalias psql='rlwrap psql'
EOF
[pg96@mambapg ~]$ source  ~/.bash_profile
[pg96@mambapg ~]$ echo $PGDATA
/pg13/pgdata
[pg96@mambapg ~]$ which pg_ctl
/pg13/pg13/bin/pg_ctl

7、收集统计信息

[pg96@mambapg ~]$ pg_ctl start
waiting for server to start....2021-12-18 13:32:16.804 CST [1425] LOG:  redirecting log output to logging collector process
2021-12-18 13:32:16.804 CST [1425] HINT:  Future log output will appear in directory "pg_log".done
server started
[pg96@mambapg ~]$ "/pg13/pg13/bin/vacuumdb" --all --analyze-in-stages
vacuumdb: processing database "mambadb": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "mambadb": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "mambadb": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

8、校验数据

[pg96@mambapg ~]$ psql
psql (13.2)
Type "help" for help.postgres=# \lList of databasesName    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------mambadb   | postgres | UTF8     | en_US.utf8 | en_US.utf8 | postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +|          |          |            |            | postgres=CTc/postgrestemplate1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | postgres=CTc/postgres+|          |          |            |            | =c/postgres
(4 rows)postgres=# \c mambadb
You are now connected to database "mambadb" as user "postgres".
mambadb=# \dList of relationsSchema |       Name       | Type  |  Owner
--------+------------------+-------+----------public | pgbench_accounts | table | postgrespublic | pgbench_branches | table | postgrespublic | pgbench_history  | table | postgrespublic | pgbench_tellers  | table | postgres
(4 rows)mambadb=# select count(*) from pgbench_accounts;count
---------1000000
(1 row)mambadb=# \q

9、删除旧集簇

其实就是执行脚本"./delete_old_cluster.sh"。待升级稳定一段时间后再进行删除,以防需要进行回滚。

[pg96@mambapg ~]$ more  ./delete_old_cluster.sh
#!/bin/shrm -rf '/pg96/pgdata'
[pg96@mambapg ~]$ rm -rf '/pg96/pgdata'

PostgreSQL版本通过pg_upgrade升级(9.6.21升级到13.2)相关推荐

  1. 从 Ubuntu 21.04 升级到 Ubuntu 21.10

    Ubuntu 21.10 "Impish Indri" 刚刚发布.如果你正在使用 Ubuntu 21.04,你应该有升级到 Ubuntu 21.10 的选项. 然而,这种推出将是逐 ...

  2. 《PostgreSQL 9.0性能调校》一一第1章 PostgreSQL版本

    第1章 PostgreSQL版本 PostgreSQL 9.0性能调校 众所周知,PostgreSQL具有丰富的功能集和非常稳定的软件版本.其默认的安全配置既被安全人员称赞又因其复杂的学习过程而被诟病 ...

  3. hbase的2.2.4版本支持哪个版本的hadoop_Hadoop 2.7 不停服升级到 3.2 在滴滴的实践

    桔妹导读:Hadoop 3的第一个稳定版本在2017年底就已经发布了,有了很多重大的改进.在HDFS方面,支持了Erasure Coding.More than 2 NameNodes.Router- ...

  4. ios11最新版本_iOS11.2.5 beta6怎么升级 哪些设备可以升级iOS11.2.5系统【详解】

    苹果向用户推送了iOS11.2.5 beta6更新,本次 iOS11.2.5 beta6如何升级 ? 哪些设备可以升级iOS11.2.5 beta6 ?这里为大家介绍下. iOS11.2.5 beta ...

  5. 【U8+】用友登录账套年度提示以前版本的数据,请使用系统管理升级。

    [问题描述] 用友U8登录账套年度提示以前版本的数据,请使用系统管理升级. 但是登陆系统管理查看版本号,已经是当前软件版本. [解决方法] 针对正常年度的数据库进行,下面语句查询. SELECT * ...

  6. 【U8】登录账套显示“账套XXX年度XXXX是以前版本的数据,请使用系统管理升级”

    用友U8 登录账套显示"账套XXX年度XXXX是以前版本的数据,请使用系统管理升级" 升级之后还是同样的提示: 解决: select * from accinformation w ...

  7. PC版QQ的“版本太旧, 无法继续使用“ 及升级后的R6030运行时错

    QQ9.2.0被提示:您的qq版本太旧,无法继续使用.升级到QQ9.6.6,提示:R6030 -CRT not initialized. 对于R6030运行时错,百度之后,得到以下几个方法: 1.安装 ...

  8. Dynamics 365 on-premises9.0版本开放下载,附上8.2升级9.0过程

    官网终于在11月2号放出了9.0 on-premises的下载链接,有人关心说这次会不会像8.2那样是一个升级补丁,显然不是,是有独立安装包的,从下载页面看到需要的操作系统是windows serve ...

  9. ATX 690 旋飞换卡飞 7速升级8速 21速升24速 方法

    如果你看这骗文章说明已经对车的配置有了的探究心,对车子的技术有一定的认识,并有可能踏上一条不归路. 已具备了解知识:  旋飞和卡飞  变速系统 指拔 前拔 后拔. 正文 很多车友会选择一款  价格便宜 ...

最新文章

  1. 操作临时表+事务级别临时表操作+会话级别临时表操作
  2. SQL 语句中 left join 后用 on 还是 where,区别大了!
  3. 计算机应用基础浙大,2014春浙大远程教育计算机应用基础-3.Word知识题
  4. Hadoop 在关机重启后,namenode启动报错
  5. You must provide a username via either --os-username or env[OS_USERNAME]
  6. 发送请求_发送soap请求调用wsdl服务
  7. python get argument_Python-Web框架-get_argument方法
  8. php查看表创建结构,MySQL 查看表结构简单命令
  9. 太扎心!10亿网民:4成初中学历,月收入超5000元不足3成
  10. 超细节!数据分析社招面试指南
  11. 三星S8原生android8.0,三星S8惧怕的全面屏机皇杀到,原生安卓8.0系统
  12. CSS margin 用法
  13. 棋牌游戏网站分析——远航游戏中心
  14. Win 7中excel 2016未响应
  15. UCanCode发布领先的大型组态建模仿真CAD与GIS开源套件2019版本
  16. 华为云CDN,是怎样拯救你的等待焦虑症
  17. 值传递,还是引用传递(实践是检验真理的唯一标准)
  18. 校招生值得去阿里淘系做技术吗?
  19. ubuntu和win10双系统安装教程
  20. 成型滤波器设计matlab,MATLAB+VHDL脉冲成型滤波器的设计 附代码

热门文章

  1. Spring Boot基础
  2. 【英语学习】【Level 08】U02 Movie Time L4 From the page to the big screen
  3. 【英语学习】【WOTD】darling 释义/词源/示例
  4. java字节型的关键字_DAY06-07 Java的基础语法-注释、标识符、关键字、数据类型
  5. python里textfield_Django:为什么我的CharField没有得到类vTextField?
  6. Unreal Engine 4 字符串转换
  7. UE4中多种颜色轮廓线的后期处理
  8. JavaScript字符串判断某个字符是否存在
  9. 「深度」千篇一律的智能音箱,为何它们就是对显示屏“不感冒”?
  10. Java设计模式(九)责任链模式 命令模式