从PostgreSQL15升级到16全过程

 更新时间:2026年06月23日 16:09:40   作者:xuejianxinokok  
这篇文章主要介绍了从PostgreSQL15升级到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 二进制文件是受支持的方法对于所有升级。

在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

2、升级, 注意不是默认端口时 需要指定-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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • PostgreSQL中json数据类型详解

    PostgreSQL中json数据类型详解

    json数据也可以被存储为text,但是 与text数据类型相比,JSON 数据类型的优势在于能强制要求每个被存储的值符合 JSON 规则,这篇文章主要介绍了PostgreSQL中json数据类型,需要的朋友可以参考下
    2023-04-04
  • PostgreSQL 向量扩展插件pgvector安装和使用

    PostgreSQL 向量扩展插件pgvector安装和使用

    本文主要介绍了PostgreSQL 向量扩展插件pgvector安装和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-05-05
  • PostgreSQL扩展bloom的具体使用

    PostgreSQL扩展bloom的具体使用

    bloom是PostgreSQL提供的一个基于布隆过滤器的索引扩展,特别适合多列任意组合查询的优化场景,具有一定的参考价值,感兴趣的可以了解一下
    2025-07-07
  • PostgreSql触发器创建、使用以及删除示例详解

    PostgreSql触发器创建、使用以及删除示例详解

    在PostgreSQL中触发器函数是一个没有参数并且返回trigger类型的函数,这篇文章主要给大家介绍了关于PostgreSql触发器创建、使用以及删除的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • 教你在PostgreSql中使用JSON字段的方法

    教你在PostgreSql中使用JSON字段的方法

    这篇文章主要介绍了干货教你在PostgreSql中使用JSON字段,通过本文可掌握在pg数据库中如何正确使用json字段,如何进行数据查询,在where子查询中如何使用,以及对json值进行聚合查询使用,需要的朋友可以参考下
    2022-11-11
  • Postgresql 赋予用户权限和撤销权限的实例

    Postgresql 赋予用户权限和撤销权限的实例

    这篇文章主要介绍了Postgresql 赋予用户权限和撤销权限的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • PostgreSQL中rank()窗口函数实用指南与示例

    PostgreSQL中rank()窗口函数实用指南与示例

    在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名,本文将详细介绍rank()函数的使用方法,并通过多个实用示例展示其在不同场景下的应用,需要的朋友可以参考下
    2025-07-07
  • PostgreSQL中扩展moddatetime的使用

    PostgreSQL中扩展moddatetime的使用

    PostgreSQL的moddatetime扩展通过触发器自动维护时间戳字段,轻量高效,适用于审计日志和多租户系统,具有一定的参考价值,感兴趣的可以了解一下
    2025-06-06
  • postgresql模糊匹配大杀器(推荐)

    postgresql模糊匹配大杀器(推荐)

    这篇文章主要介绍了postgresql模糊匹配大杀器,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • PostgreSQL创建自增序列、查询序列及使用序列代码示例

    PostgreSQL创建自增序列、查询序列及使用序列代码示例

    数据库中主键的生成一般是通过序列来生成,下面这篇文章主要给大家介绍了关于PostgreSQL创建自增序列、查询序列及使用序列的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11

最新评论