从 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 二进制文件是受支持的方法对于所有升级。

相关推荐
编程洪同学31 分钟前
Spring Boot 中实现自定义注解记录接口日志功能
android·java·spring boot·后端
GraduationDesign1 小时前
基于SpringBoot的蜗牛兼职网的设计与实现
java·spring boot·后端
颜淡慕潇1 小时前
【K8S问题系列 | 20 】K8S如何删除异常对象(Pod、Namespace、PV、PVC)
后端·云原生·容器·kubernetes
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS安康旅游网站(JAVA毕业设计)
java·vue.js·spring boot·后端·kafka·开源·旅游
搬码后生仔3 小时前
将 ASP.NET Core 应用程序的日志保存到 D 盘的文件中 (如 Serilog)
后端·asp.net
Suwg2093 小时前
《手写Mybatis渐进式源码实践》实践笔记(第七章 SQL执行器的创建和使用)
java·数据库·笔记·后端·sql·mybatis·模板方法模式
凡人的AI工具箱4 小时前
每天40分玩转Django:Django文件上传
开发语言·数据库·后端·python·django
spcodhu4 小时前
在 Ubuntu 上搭建 MinIO 服务器
linux·后端·minio
小码编匠4 小时前
2024 年各编程语言运行百万并发任务需多少内存?
java·后端·python
小码编匠4 小时前
C# 实现多线程启动停止暂停继续
后端·c#·.net