1. 简介

该文档中描述的方式针对源码安装的数据进行升级

  1. 操作过程
1. 说明
	a. 当前数据库版本为11.22,升级后为15.8
2. 备份原数据库
3. 安装目标版本
  [root@olinux73_model sws]# tar -zxf postgresql-15.8.tar.gz
  [root@olinux73_model sws]# cd postgresql-15.8/
  [root@olinux73_model postgresql-15.8]# mkdir -p /opt/app/pg15
  [root@olinux73_model postgresql-15.8]# chown -R postgres:postgres /opt/app/pg15
  [root@olinux73_model postgresql-15.8]# ./configure --prefix=/opt/app/pg15 --with-pgport=5432 --with-openssl  --with-blocksize=32 --with-readline
  [root@olinux73_model postgresql-15.8]# make && make install

  [root@olinux73_model ~]# mkdir /opt/app/pgdata
  [root@olinux73_model ~]# chown -R postgres:postgres /opt/app/pgdata/

  [root@olinux73_model postgresql-15.8]# su - postgres 
  -bash-4.2$  /opt/app/pg15/bin/initdb  -D /opt/app/pgdata -U postgres

4. 兼容性测试
  [root@olinux73_model pg_backup]# systemctl stop  postgresql.service 
  -bash-4.2$ /opt/app/pg15/bin/pg_upgrade --old-datadir=/u01/app/pgdata --new-datadir /opt/app/pgdata --old-bindir /u01/app/pgsql/bin --new-bindir /opt/app/pg15/bin  --check
  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 system-defined composite types in user tables  ok
  Checking for reg* data types in user tables                 ok
  Checking for contrib/isn with bigint-passing mismatch       ok
  Checking for removed "abstime" data type in user tables     ok
  Checking for removed "reltime" data type in user tables     ok
  Checking for removed "tinterval" data type in user tables   ok
  Checking for user-defined encoding conversions              ok
  Checking for user-defined postfix operators                 ok
  Checking for incompatible polymorphic functions             ok
  Checking for tables WITH OIDS                               ok
  Checking for invalid "sql_identifier" user columns          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*
5. 执行升级
  -bash-4.2$ /opt/app/pg15/bin/pg_upgrade --old-datadir=/u01/app/pgdata --new-datadir /opt/app/pgdata --old-bindir /u01/app/pgsql/bin --new-bindir /opt/app/pg15/bin
  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 system-defined composite types in user tables  ok
  Checking for reg* data types in user tables                 ok
  Checking for contrib/isn with bigint-passing mismatch       ok
  Checking for removed "abstime" data type in user tables     ok
  Checking for removed "reltime" data type in user tables     ok
  Checking for removed "tinterval" data type in user tables   ok
  Checking for user-defined encoding conversions              ok
  Checking for user-defined postfix operators                 ok
  Checking for incompatible polymorphic functions             ok
  Checking for tables WITH OIDS                               ok
  Checking for invalid "sql_identifier" 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_xact to new server                           ok
  Setting oldest XID for new cluster                          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
  Copying user relation files                                 
                                ok
  Setting next OID for new cluster                            ok
  Sync data directory to disk                                 ok
  Creating script to delete old cluster                       ok
  Checking for extension updates                              ok

  Upgrade Complete
  ----------------
  Optimizer statistics are not transferred by pg_upgrade.
  Once you start the new server, consider running:
    /opt/app/pg15/bin/vacuumdb --all --analyze-in-stages

  Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
6. 修改环境变量
  [root@olinux73_model ~]# vim /home/postgres/.bash_profile
  export PGHOME=/opt/app/pg15
  export PGDATA=/opt/app/pgdata
  export PGPORT=5432
  export PGPASSWORD=123456
  export PATH=$PGHOME/bin:$PATH
  export MANPATH=$PGHOME/share/man:$MANPATH
  export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
  export SCRIPTS_DIR=$SCRIPTS_DIR
  export LANG=en_US.UTF-8
  export DATE=`date +"%Y%m%d%H%M"`

	[root@olinux73_model ~]# source /home/postgres/.bash_profile
7. 启动数据库
  -bash-4.2$   /opt/app/pg15/bin/pg_ctl -D /opt/app/pgdata -l logfile start
  waiting for server to start.... done
  server started
8. 收集统计信息
  -bash-4.2$ /opt/app/pg15/bin/vacuumdb --all --analyze-in-stages
  vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
  vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
  vacuumdb: processing database "testdb": Generating minimal optimizer statistics (1 target)
  vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
  vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
  vacuumdb: processing database "testdb": Generating medium optimizer statistics (10 targets)
  vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
  vacuumdb: processing database "template1": Generating default (full) optimizer statistics
  vacuumdb: processing database "testdb": Generating default (full) optimizer statistics
9. 删除旧数据
	-bash-4.2$ ./delete_old_cluster.sh    #会删除老的数据目录
10. 查看数据
  postgres=# \c testdb 
  psql (11.22, server 15.8)
  WARNING: psql major version 11, server major version 15.
       Some psql features might not work.
  You are now connected to database "testdb" as user "postgres".
  testdb=# select * from tb01;
   id | name 
  ----+------
    1 | a
  (1 row)