最方便快捷的在本机升级。但是要求有足够的空间。因为文件是被拷过去的。所以剩余空间一定要足够拷贝一份完整数据的。
PostgreSQL的主要版本定期添加新功能,这些功能经常改变系统表的布局,但是内部数据存储格式很少改变。pg_upgrade通过创建新的系统表和简单地重用旧的用户数据文件来执行快速升级。如果未来的主要版本更改了数据存储格式,使旧数据格式无法读取,则pg_upgrade 将无法用于此类升级。(社区将试图避免这种情况。)
实验:
环境: CentOS 7 + PG 10.5 -> PG 11.1
1. 将原安装目录改名
mv /usr/local/pgsql /usr/local/pgsql.old
2. 安装新版本
可以通过Binary 或 Source 方式安装。安装细节不再赘述。
Source安装时,将文件安装到新的目录。
make prefix=/usr/local/pgsql.new install
如果是Binary安装,安装之后,做链接到新目录。ln -s /usr/pgsql-11 /usr/local/pgsql.new,并链接DATA目录。
ln -s /var/lib/pgsql/11/data /usr/local/pgsql.new/data
最后记得将所有相关的文件夹属组属主改为 postgres。
3. 初始化新版本
su - postgres
cd /usr/local/pgsql.new/bin
./initdb -D /usr/local/pgsql.old/data/
4. 安装相关的shared_preload_libraries 和 extension
5. 确认新版本是否可以启动。启动之后再关闭
service postgresql-11 start
service postgresql-11 stop
6. 停止两个版本的服务
service postgresql-10 stop
service postgresql-11 stop
7. 升级
-bash-4.2$ cd /usr/local/pgsql.new/bin/
-bash-4.2$ ./pg_upgrade --old-bindir=/usr/local/pgsql.old/bin/ --new-bindir=/usr/local/pgsql.new/bin/ --old-datadir=/usr/local/pgsql.old/data/ --new-datadir=/usr/local/pgsql.new/data/ --jobs=6
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
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
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 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
Creating script to delete old cluster 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
-bash-4.2$ ./analyze_new_cluster.sh
This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy. When it is done, your system will
have the default level of optimizer statistics.
If you have used ALTER TABLE to modify the statistics target for
any tables, you might want to remove them and restore them after
running this script because they will delay fast statistics generation.
If you would like default statistics as quickly as possible, cancel
this script and run:
"/usr/local/pgsql.new/bin/vacuumdb" --all --analyze-only
vacuumdb: processing database "dbtest01": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "hive_metadata_test": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "mytest": 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 "dbtest01": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "hive_metadata_test": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "mytest": 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 "dbtest01": Generating default (full) optimizer statistics
vacuumdb: processing database "hive_metadata_test": Generating default (full) optimizer statistics
vacuumdb: processing database "mytest": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics
Done
-bash-4.2$ ./delete_old_cluster.sh
6. 恢复原配置文件
将old中的pg_hba 和 postgresql.conf 拷贝到新的data中。
7. 启动新版本
service postgresql-11 start
8. 收集统计信息(在升级之后,有个脚本,已经执行过的就不用执行了)
由于优化器统计信息不会通过 pg_upgrade 进行传输,因此将指示运行一个命令,以便在升级结束时重新生成该信息。
./analyze_new_cluster.sh
9. 删除旧的数据(在升级之后,有个脚本,已经执行过的就不用执行了)
只会删除 /usr/local/pgsql.old/data 的链接,数据还在 /var/lib/pgsql/10/data/ 中。
./delete_old_cluster.sh