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

相关推荐
Absinthe_苦艾酒1 小时前
Spring 容器相关的核心注解
java·后端·spring
why1511 小时前
百度golang开发一面
开发语言·网络·后端·golang
kngines9 小时前
【PostgreSQL数据分析实战:从数据清洗到可视化全流程】4.2 数据类型转换(CAST函数/自定义函数)
数据库·postgresql·数据分析·filter·自定义函数·cte
Q_Boom9 小时前
前端跨域问题怎么在后端解决
java·前端·后端·spring
qq_38413684410 小时前
SpringBoot的启动流程
java·spring boot·后端
月忆36412 小时前
Go语言接口实现面对对象的三大特征
开发语言·后端·golang
程序员曼布12 小时前
RabbitMQ 深度解析:从核心组件到复杂应用场景
java·开发语言·后端·rabbitmq
计算机学姐15 小时前
基于SpringBoot的同城宠物照看管理系统
java·vue.js·spring boot·后端·mysql·mybatis·宠物
探索未来 航行现在16 小时前
Go语言--语法基础4--基本数据类型--类型转换
开发语言·后端·golang
我命由我1234516 小时前
C++ - 数据容器之 forward_list(创建与初始化、元素访问、容量判断、元素遍历、添加元素、删除元素)
c语言·开发语言·c++·后端·visualstudio·visual studio·后端开发