postgresql upgrade 10 to 13 — by Firsouler 2021.1.19
1.基础环境配置
安装主备
#创建相关目录
mkdir -p /pgdata/pgsql13
mkdir -p /pgdata/data13
su - postgres
gunzip postgresql-13.1.tar.gz
tar -xvf postgresql-13.1.tar
#编译命令
./configure -help
#prefix=prefix 指定安装目录,默认的安装目录为"/usr/local/pgsql"
#includedir=DIR 指定C和C++的头文件目录,默认的安装目录为"PREFIX/include"
#with-pgort=PORTNUM 指定初始化数据目录时的默认端口号
#with-blocksize=BLOCKSIZE 指定数据文件的块大小,默认的是8kb,如果再olap场景,可适当增加到32kb,oltp建议8kb
#with-segsize=SEGSIZE 指定单个数据文件的大小,默认为1GB
#with-wal-blocksize=BLOCKSIZE 指定wal文件的块大小
#with-wal-segment=SEGSIZE 指定单个wal文件的大小,默认16MB
#with--xxx--size 四个参数只能在编译的时候指定,如果修改,需要重新将数据导出导入
开始安装
#查看端口号
netstat -a | grep PGSQL
#建议备份数据库,并停止数据库
pg_dumpall > backup2021.sql
pg_ctl stop
#开始配置
./configure --prefix=/pgdata/pgsql13 --with-pgport=5432
#编译
make
#编译,如果希望一次性将文档和附加模块全部进行编译和安装,可以使用make world,推荐该命令
make world #or make /make all
#安装
make install-world #or make insall
#初始化数据目录 注意环境变量 LANG
/pgdata/pgsql13/bin/initdb -E UTF-8 -D /pgdata/data13
#查看数据库LC_COLLATE,LC_CTYPE,数据库字符编码
\l
--or
select * from pg_database;
--更换
UPDATE pg_database SET datcollate='en_US.UTF-8', datctype='en_US.UTF-8' where datname='postgres';
2.升级
检查版本兼容性:
/pgdata/pgsql13/bin/pg_upgrade -b /pgdata/pgsql/bin -B /pgdata/pgsql13/bin -d /pgdata/data -D /pgdata/data13 -k -c
#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 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*
开始升级
/pgdata/pgsql13/bin/pg_upgrade \
--old-datadir=/pgdata/data \
--new-datadir=/pgdata/data13 \
--old-bindir=/pgdata/pgsql/bin \
--new-bindir=/pgdata/pgsql13/bin \
--old-options '-c config_file=/pgdata/data/postgresql.conf' \
--new-options '-c config_file=/pgdata/data13/postgresql.conf'
#输出样例
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 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 analyze new cluster ok
WARNING: user-defined tablespace locations should not be inside the data directory, e.g. /pgdata/data/pg_tbs/tbs_mydb
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
./analyze_new_cluster.sh
Could not create a script to delete the old cluster's data files
because user-defined tablespaces or the new cluster's data directory
exist in the old cluster directory. The old cluster's contents must
be deleted manually.
#注意,不能删除老的目录,因为有用户自定义的相关表空间等
#收集新库统计信息
pg_start
./analyze_new_cluster.sh
后续工作
--修改环境变量,查看版本
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 13.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16), 64-bit
(1 row)