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

2021/12/19 2:20:22

本文主要是介绍PostgreSQL版本通过pg_upgrade升级(9.6.21升级到13.2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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 ... ok

initdb: 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=# \d
              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)

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 schemas
                                                            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

If 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 cluster
                                                            ok
Adding ".old" suffix to old global/pg_control               ok

If 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 files
                                                            ok
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                                   ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
[pg96@mambapg ~]$ more ./analyze_new_cluster.sh
#!/bin/sh

echo '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.'
echo

echo '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.'
echo

echo '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
echo

echo 'Done'
[pg96@mambapg ~]$ more  ./delete_old_cluster.sh
#!/bin/sh

rm -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=postgres

alias 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=# \l
                                 List of databases
   Name    |  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/postgres
 template1 | 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=# \d
              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)

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/sh

rm -rf '/pg96/pgdata'
[pg96@mambapg ~]$ rm -rf '/pg96/pgdata'


这篇关于PostgreSQL版本通过pg_upgrade升级(9.6.21升级到13.2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程