pg_upgrade数据库版本升级

环境:
OS:Centos 7
升级方式:原地升级
旧版本:pg12
新版本:pg14

##################在服务器上安装新版本#####################
1.yum安装
我这里安装pg14版本
[root@localhost ]# yum install postgresql14-server
[root@localhost ]# yum install postgresql14-contrib

 

2.创建数据存储目录
[root@localhost bin]#mkdir -p /opt/pg14/data
[root@localhost bin]#mkdir -p /opt/pg14/log
[root@localhost bin]#mkdir -p /opt/pg14/archivelog
[root@localhost bin]#chown -R postgres:postgres /opt/pg14
[root@localhost bin]#chmod 0700 /opt/pg14/data

 

3.初始化数据库
su - postgres
/usr/pgsql-14/bin/initdb -D /opt/pg14/data

 

4.修改系统服务启动参数
su - root
[root@localhost postgresql-13.service.d]# ls -al /usr/lib/systemd/system/postgresql-14.service
-rw-r--r--. 1 root root 1764 Aug 10 06:06 /usr/lib/systemd/system/postgresql-14.service

vi /usr/lib/systemd/system/postgresql-14.service
修改为Environment=PGDATA=/opt/pg14/data/

到这里可以尝试启动新版本的数据库,但是需要修改端口号(其他参数不修改),不能与原来的一样,否则报错

比如我这里修改成如下:
su - postgres
vi /opt/pg14/data/postgresql.conf
port = 15432

 

5.尝试启动
[root@localhost]#systemctl daemon-reload
[root@localhost]#systemctl start postgresql-14

确定可以启动后,将上面修改的参数修改回来,port = 5432

 

############################升级##########################

1.停掉新旧版本的数据库
[root@localhost ~]#systemctl stop postgresql-12
[root@localhost ~]#systemctl stop postgresql-14

2.升级检查
su - postgres
[postgres@localhost bin]$ /usr/pgsql-14/bin/pg_upgrade --old-datadir /opt/pg12/data/ --new-datadir /opt/pg14/data --old-bindir /usr/pgsql-12/bin/ --new-bindir /usr/pgsql-14/bin/ --check

could not open log file "pg_upgrade_internal.log": Permission denied
Failure, exiting

解决办法,修改程序目录权限为postgres用户
[root@localhost usr]# chown -R postgres:postgres /usr/pgsql-14

[postgres@localhost ~]$ /usr/pgsql-14/bin/pg_upgrade --old-datadir /opt/pg12/data/  --new-datadir /opt/pg14/data  --old-bindir /usr/pgsql-12/bin/  --new-bindir /usr/pgsql-14/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 user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             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*

 

4.执行升级

[postgres@localhost ~]$ /usr/pgsql-14/bin/pg_upgrade --old-datadir /opt/pg12/data/  --new-datadir /opt/pg14/data  --old-bindir /usr/pgsql-12/bin/  --new-bindir /usr/pgsql-14/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 user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             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:
    /usr/pgsql-14/bin/vacuumdb --all --analyze-in-stages

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

 

上面最后面的提示是要求执行两个脚本


delete_old_cluster.sh的内容如下:
[postgres@localhost ~]$ more delete_old_cluster.sh
#!/bin/sh

rm -rf '/opt/pg12/data'


注意这里升级完成后,pg_hba.conf和postgresql.conf配置文件都是新的,都需要重新配置

 

 

--link模式的升级方式,升级完成后有这个提示:

If you want to start the old cluster, you will need to remove
the ".old" suffix from /opt/pg12/data/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.

若是启动旧版本的pg,需要将如下文件改名:
[postgres@localhost global]$ mv pg_control.old pg_control

 

 

5.启动新版本的数据库
[root@localhost]#systemctl start postgresql-14

同时禁用之前的老版本数据库
[root@localhost]#systemctl disable postgresql-12

 

6.修改postgres用户的环境变量

[postgres@localhost ~]$ more .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
PGHOME=/usr/pgsql-14
PGDATA=/opt/pg14/data
PATH=$PGHOME/bin:$PATH:$HOME/.local/bin:$HOME/bin

export PATH
export PGHOST=/tmp

 

7.数据验证

[postgres@localhost ~]$ psql
psql (14.6)
Type "help" for help.

postgres=# \c db_test;
You are now connected to database "db_test" as user "postgres".
db_test=# select * from tb_test;
 id | name  
----+-------
  1 | name1
  2 | name2
  3 | name3
  4 | name4
  5 | name5
(5 rows)

 

8.执行脚本
根据实际情况执行

[postgres@localhost ~]$ /usr/pgsql-14/bin/vacuumdb --all --analyze-in-stages
vacuumdb: processing database "db_test": 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 "db_test": 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 "db_test": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

 

[postgres@localhost ~]$sh /home/postgres/delete_old_cluster.sh

 


9.修改数据库参数
如下两个配置文件都需要进行修改,这里省略,或是把旧版本的配置文件拷贝过来直接使用
pg_hba.conf
postgresql.conf

 

 

安装了插件的处理方式:

升级检查

 

[postgres@localhost ~]$ /usr/pgsql-14/bin/pg_upgrade --old-datadir /opt/pg12/data/  --new-datadir /opt/pg14/data  --old-bindir /usr/pgsql-12/bin/  --new-bindir /usr/pgsql-14/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 user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             ok
Checking for presence of required libraries                 fatal

Your installation references loadable libraries that are missing from the
new installation.  You can add these libraries to the new installation,
or remove the functions using them from the old installation.  A list of
problem libraries is in the file:
    loadable_libraries.txt

Failure, exiting

[postgres@localhost ~]$ more loadable_libraries.txt
could not load library "$libdir/mysql_fdw": ERROR:  could not access file "$libdir/mysql_fdw": No such file or direct
ory
In database: postgres
could not load library "$libdir/postgis-3": ERROR:  could not access file "$libdir/postgis-3": No such file or direct
ory
In database: postgres
could not load library "$libdir/postgis_raster-3": ERROR:  could not access file "$libdir/postgis_raster-3": No such 
file or directory
In database: postgres
could not load library "$libdir/postgis_sfcgal-3": ERROR:  could not access file "$libdir/postgis_sfcgal-3": No such 
file or directory
In database: postgres
could not load library "$libdir/postgis_topology-3": ERROR:  could not access file "$libdir/postgis_topology-3": No s
uch file or directory
In database: postgres
could not load library "$libdir/address_standardizer-3": ERROR:  could not access file "$libdir/address_standardizer-
3": No such file or directory
In database: postgres

 

 

解决办法:
安装新版本的插件
[root@localhost extension]#yum install mysql_fdw_14.x86_64
[root@localhost extension]#yum install postgis32_14.x86_64 ##这里的pis版本需要与旧版本一致,都选择gis32版本
升级完成后使用\dx查看是否有之前的插件,没有的话需要安装这些插件
db_test=# \dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(1 row)

postgres=# create extension postgres_fdw;
postgres=# create extension dblink;
postgres=# create extension mysql_fdw;


postgres=# create extension postgis;
postgres=# create extension postgis_raster;
postgres=# create extension postgis_topology;
postgres=# create extension postgis_sfcgal;
postgres=# create extension fuzzystrmatch;
postgres=# create extension address_standardizer;
postgres=# create extension address_standardizer_data_us;
postgres=# create extension postgis_tiger_geocoder;

 

 

 

posted @ 2022-11-23 16:31  slnngk  阅读(671)  评论(0编辑  收藏  举报