从 PostgreSQL 15 升级到 16

从 PostgreSQL 15 升级到 16

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

升级步骤

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

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

停止所有 PostgreSQL 服务

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

$ brew services stop postgresql@15
$ brew services stop postgresql@16

运行 pg_upgrade 命令

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

$ pg_upgrade \
-b <old binary directory> \
-d <old data directory> \
-B <new binary directory> \
-D <new data directory>

例如:

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

$ /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

这会产生以下输出:

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

启动服务

$ brew services start postgresql@16

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

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

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

故障排除

Failed: Incorrect version

$ 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

源集群没有完全关闭

$ /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

升级期间区域设置失败

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

$ /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

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

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 环境,例如:

$ cat ~/.zshrc

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

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


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


相关文章:

使用pg_upgrade检查Postgres升级


在windows升级 pg的错误

    1. 必须用 新版本16 的软件 先初始化新库
      用pg_upgrade程序,相当于同时运行两个数据库,互相导文件;
D:\soft\pgsql\soft\bin\initdb.exe -D D:\soft\pgsql\data\54333 -E UTF-8 --locale=chs -U postgres -W
    1. 升级, 注意不是默认端口时 需要指定-p -P -U
pg_upgrade -p 5433 -U postgres -b D:/soft/pgsql/soft1/bin -d D:/soft/pgsql/data/5433  -P 54333  -B D:/soft/pgsql/soft/bin -D D:/soft/pgsql/data/54333
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值