You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL10升级至12时pg_upgrade检测缺失依赖库报错如何解决

PostgreSQL 10 升级至 12 完整操作步骤

1. 前置准备

  • 全量备份PostgreSQL 10的所有数据,可使用pg_basebackup或者pg_dumpall命令备份,避免升级失败数据丢失
  • 停止当前运行的PostgreSQL 10服务:
    CentOS/RHEL系:systemctl stop postgresql-10
    Debian/Ubuntu系:systemctl stop postgresql@10-main
  • 安装PostgreSQL 12及对应扩展包:
    CentOS/RHEL系:
    yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
    yum install postgresql12-server postgresql12-contrib -y
    
    Debian/Ubuntu系:
    apt install gnupg2 -y
    wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
    echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
    apt update
    apt install postgresql-12 postgresql-12-contrib -y
    
    你遇到的依赖库缺失报错,就是因为未安装postgresql12-contrib包,ltree、pg_trgm、uuid-ossp都属于该包内置的扩展组件
  • 初始化PostgreSQL 12的数据库集群(不要启动服务):
    CentOS/RHEL系:/usr/pgsql-12/bin/postgresql-12-setup initdb
    Debian/Ubuntu系安装完成会自动初始化,无需手动执行。

2. 升级前预校验

  • 切换至postgres用户:su - postgres
  • 执行pg_upgrade预校验命令,确保所有检查项通过:
/usr/pgsql-12/bin/pg_upgrade \
-b /usr/pgsql-10/bin/ \
-B /usr/pgsql-12/bin/ \
-d /var/lib/pgsql/10/data/ \
-D /var/lib/pgsql/12/data/ \
--check

注:如果是Debian/Ubuntu系统,二进制路径默认是/usr/lib/postgresql/10/bin和/usr/lib/postgresql/12/bin,数据路径默认是/var/lib/postgresql/10/main和/var/lib/postgresql/12/main,请对应修改路径参数

  • 若校验过程中出现其他依赖库缺失的报错,只需安装PostgreSQL 12对应的扩展包后重新执行校验即可。

3. 正式执行升级

  • 确认预校验所有项显示正常后,执行正式升级命令:
/usr/pgsql-12/bin/pg_upgrade \
-b /usr/pgsql-10/bin/ \
-B /usr/pgsql-12/bin/ \
-d /var/lib/pgsql/10/data/ \
-D /var/lib/pgsql/12/data/
  • 等待升级执行完成,界面会输出升级成功的提示。

4. 升级后验证操作

  • 启动PostgreSQL 12服务:
    CentOS/RHEL系:systemctl start postgresql-12
    Debian/Ubuntu系:systemctl start postgresql@12-main
  • 执行升级后一致性校验脚本:./analyze_new_cluster.sh
  • 验证数据库版本:登录数据库执行SELECT version();确认返回12.x版本
  • 验证业务数据、扩展功能是否正常可用,确认ltree、pg_trgm、uuid-ossp扩展是否正常加载:
SELECT * FROM pg_extension WHERE extname IN ('ltree','pg_trgm','uuid-ossp');

5. 收尾操作

  • 确认所有功能正常后,执行旧集群删除脚本释放空间:./delete_old_cluster.sh
  • 配置PostgreSQL 12开机自启:
    CentOS/RHEL系:systemctl enable postgresql-12
    Debian/Ubuntu系:systemctl enable postgresql@12-main

内容的提问来源于stack exchange,提问作者Shubham Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 16:36:04