从 PostgreSQL 15 升级到 16

从 PostgreSQL 15 升级到 16

注意:如果您需要版本14至15的升级步骤,可以在here找到。

升级步骤

这些步骤假设您已经通过 brew 安装了 PostgreSQL 版本 15.4 和 16.0。如果您有不同的次要版本,请在随后的 pg_upgrade 命令行中相应地替换它们。

通常的注意事项和警告适用于此:在继续升级之前备份数据,并测试备份以确保它们有效。

停止所有 PostgreSQL 服务

除非两个集群的 postmaster 都停止,否则升级不会继续。

shell 复制代码
$ brew services stop postgresql@15
$ brew services stop postgresql@16

运行 pg_upgrade 命令

作为参考,pg_upgrade 命令需要旧的和新的bin目录,以及旧的和新的数据目录:

sql 复制代码
$ pg_upgrade \
-b <old binary directory> \
-d <old data directory> \
-B <new binary directory> \
-D <new data directory>

例如:

注意,我们正在运行 16 目录中的 pg_upgrade 二进制文件,而不是 $PATH 中的 15.4 二进制文件。

shell 复制代码
$ /opt/homebrew/Cellar/postgresql@16/16.0_1/bin/pg_upgrade \
-b /opt/homebrew/Cellar/postgresql@15/15.4/bin \
-d /opt/homebrew/var/postgresql@15 \
-B /opt/homebrew/Cellar/postgresql@16/16.0_1/bin \
-D /opt/homebrew/var/postgresql@16

这会产生以下输出:

sql 复制代码
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
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/homebrew/Cellar/postgresql@16/16.0_1/bin/vacuumdb --all --analyze-in-stages

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

启动服务

shell 复制代码
$ brew services start postgresql@16

如果一切正常,您可以删除旧集群的数据目录。您可能还想运行上面日志中提到的建议的 vacuumdb 命令。

将新版本的二进制文件添加到您的 $PATH

shell 复制代码
$ echo 'export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"' >> ~/.zshrc
$ source ~/.zshrc

故障排除

Failed: Incorrect version

shell 复制代码
$ pg_upgrade -b /opt/homebrew/Cellar/postgresql@15/15.4/bin -d postgresql@15 -B /opt/homebrew/Cellar/postgresql@16/16.0_1/bin -D postgresql@16

check for "/opt/homebrew/Cellar/postgresql@16/16/bin/postgres" failed: incorrect version: found "postgres (PostgreSQL) 16 (Homebrew)", expected "postgres (PostgreSQL) 15.4 (Homebrew)"
Failure, exiting

如果您看到此错误,则可能是由于运行旧版本 (14) 而不是新版本 (15) 附带的 pg_upgrade 二进制文件造成的。使用版本 15 的 pg_upgrade 二进制文件的绝对路径来运行正确的二进制文件。

The source cluster was not shut down cleanly

源集群没有完全关闭

shell 复制代码
$ /opt/homebrew/Cellar/postgresql@16/16.0_1/bin/pg_upgrade -b /opt/homebrew/Cellar/postgresql@15/15.4/bin -d postgresql@15 -B /opt/homebrew/Cellar/postgresql@16/16.0_1/bin -D postgresql@16
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok

The source cluster was not shut down cleanly.
Failure, exiting

再次启动旧集群,并在关闭之前允许其从最新日志中恢复。现在照常继续执行 pg_upgrade 命令。

Locale failure during upgrade

升级期间区域设置失败

阅读下面的日志并不能提供有关此问题的足够详细信息,但它引用了另一个日志文件(以粗体突出显示):

shell 复制代码
$ /opt/homebrew/Cellar/postgresql@16/16.0_1/bin/pg_upgrade -b /opt/homebrew/Cellar/postgresql@15/15.4/bin -d postgresql@15 -B /opt/homebrew/Cellar/postgresql@16/16.0_1/bin -D postgresql@16
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
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok

*failure*
Consult the last few lines of "postgresql@16/pg_upgrade_output.d/20230816T122031.100/log/pg_upgrade_server.log" for
the probable cause of the failure.

connection to server on socket "/opt/homebrew/var/.s.PGSQL.50432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

could not connect to target postmaster started with the command:
"/opt/homebrew/Cellar/postgresql@16/16.0_1/bin/pg_ctl" -w -l "postgresql@16/pg_upgrade_output.d/20230816T122031.100/log/pg_upgrade_server.log" -D "postgresql@16" -o "-p 50432 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c vacuum_defer_cleanup_age=0  -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/opt/homebrew/var'" start
Failure, exiting

从那里我们可以查看这个日志文件:

shell 复制代码
command: "/opt/homebrew/Cellar/postgresql@16/16.0_1/bin/pg_ctl" -w -l "postgresql@16/pg_upgrade_output.d/20230816T122031.100/log/pg_upgrade_server.log" -D "postgresql@16" -o "-p 50432 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c vacuum_defer_cleanup_age=0  -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/opt/homebrew/var'" start >> "postgresql@16/pg_upgrade_output.d/20230816T122031.100/log/pg_upgrade_server.log" 2>&1
waiting for server to start....2023-08-16 12:20:31.898 SAST [1700] LOG:  starting PostgreSQL 16 (Homebrew) on aarch64-apple-darwin22.4.0, compiled by Apple clang version 14.0.3 (clang-1403.0.22.14.1), 64-bit
2023-08-16 12:20:31.898 SAST [1700] LOG:  listening on Unix socket "/opt/homebrew/var/.s.PGSQL.50432"
2023-08-16 12:20:31.898 SAST [1700] FATAL:  postmaster became multithreaded during startup
2023-08-16 12:20:31.898 SAST [1700] HINT:  Set the LC_ALL environment variable to a valid locale.
2023-08-16 12:20:31.899 SAST [1700] LOG:  database system is shut down
 stopped waiting
pg_ctl: could not start server
Examine the log output.

该提示提供了必要的提示 -- the locale is not set correctly,升级无法继续。适当调整您的 shell 环境,例如:

shell 复制代码
$ cat ~/.zshrc

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

加载新环境并再次尝试升级过程。


此过程对于 PostgreSQL 的任何未来版本都应该足够了。它不依赖于外部维护的脚本(例如已弃用的 brew postgresql-upgrade-database 脚本),并且可以针对任何其他环境进行调整(只要更改此处使用的路径),因为 pg_upgrade 二进制文件是受支持的方法对于所有升级。

相关推荐
m0_74823364几秒前
RabbitMQ 进阶
android·前端·后端
m0_748238639 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
张声录11 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
一小路一10 小时前
从0-1学习Mysql第五章: 索引与优化
数据库·后端·学习·mysql·面试
mah66610 小时前
SpringBoot项目连接Oracle视图报错整理
spring boot·后端·oracle
IT学长编程11 小时前
计算机毕业设计 基于SpringBoot的智慧社区管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·后端·毕业设计·课程设计·论文笔记·1024程序员节
Asthenia041211 小时前
详细分析 `@@` 和 `${}` 的来源及启动配置
后端
奋斗的小方11 小时前
Springboot基础篇(3):Bean管理
java·spring boot·后端