资讯专栏INFORMATION COLUMN

升级 postgresql

learn_shifeng / 3497人阅读

摘要:但如果涉及到跨大版本升级比如升级到,这种直接替换软件就不行了,因为跨版本的内部存储形式发生了变化官方对于升级提供了种方法,这里迁移我使用了来进行处理细节可以参考官方文档。

1 场景描述

最近使用 postgresql 命令行比较多,就找了个类似 mycli 工具,pgcli,这个是针对 postgresql 的,兴冲冲的安装了

brew install pgcli

没想到这货自动帮我升级了 postgresql,我原来使用的是 9.5.7, 自动帮我升级到了 9.6.3

查看升级日志 pg_upgrade_server.log

pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.5, which is not compatible with this version 9.6.3.
 stopped waiting
pg_ctl: could not start server
Examine the log output.

这是需要迁移数据的节奏啊,迁移之前需要的明白点什么

postgresql 中的升级,如果针对小版本的升级,比如 9.6.1 升级到 9.6.3(当前的最新版本),只需要用 9.6.3 版本的软件替换 9.6.1 版本的软件即可,不需要做额外的操作,因为整个大版本是相互兼容的,内部存储形式也是兼容的。但如果涉及到跨大版本升级比如 9.5.7 升级到 9.6.3,这种直接替换软件就不行了,因为跨版本的内部存储形式发生了变化

官方对于升级提供了 3 种方法,这里迁移我使用了 pg_upgrade 来进行处理, 细节可以参考官方文档。

再进行下一步之前,把 postgresql 服务关掉,执行如下命令

brew services stop postgresql
2 如何迁移数据 2.1 备份数据
mv /usr/local/var/postgresql /usr/local/var/postgresql.old

2.2 使用新版本初始化新的数据库
initdb /usr/local/var/postgres -E utf8 --locale=zh_CN.UTF-8

运行结果

The files belonging to this database system will be owned by user "allen".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.UTF-8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

creating directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start
2.3 迁移旧数据

pg_upgrade 语法简单如下:

pg_upgrade -b 旧版本的bin目录 -B 新版本的bin目录 -d 旧版本的数据目录 -D 新版本的数据目录 [其他选项...]

然后运行迁移命令

pg_upgrade -b /usr/local/Cellar/postgresql@9.5/9.5.7/bin -B /usr/local/bin  -d /usr/local/var/postgres.old -D /usr/local/var/postgres  

运行结果

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 reg* system OID user data types                ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for roles starting with "pg_"                      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

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 on the new cluster                        ok
Deleting files from new pg_clog                             ok
Copying old pg_clog to new server                           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 analyze new cluster                      ok
Creating script to delete old cluster                       ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster"s data files:
    ./delete_old_cluster.sh

最后提示生成两个脚本,一个是 analyze_new_cluster.sh,需要在新版本中执行,用来收集统计信息,另一个是 delete_old_cluster.sh,用来删除旧版本集群数据,当然为了安全起见可以等系统运行几天没问题再来删除。

运行下 analyze_new_cluster.sh 脚本

./analyze_new_cluster.sh

运行结果如下:

This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy.  When it is done, your system will
have the default level of optimizer statistics.

If you have used ALTER TABLE to modify the statistics target for
any tables, you might want to remove them and restore them after
running this script because they will delay fast statistics generation.

If you would like default statistics as quickly as possible, cancel
this script and run:
    "/usr/local/bin/vacuumdb" --all --analyze-only

vacuumdb: processing database "activity_tool": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "allen": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "cw": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "djexample": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "finance": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "iop": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "learn": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "servicemall": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "store": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "test_store": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "uio": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "vbdev": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "vbdevelop": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "xiuzan": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "xzdevelop": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "activity_tool": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "allen": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "cw": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "djexample": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "finance": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "iop": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "learn": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "servicemall": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "store": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "test_store": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "uio": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "vbdev": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "vbdevelop": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "xiuzan": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "xzdevelop": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "activity_tool": Generating default (full) optimizer statistics
vacuumdb: processing database "allen": Generating default (full) optimizer statistics
vacuumdb: processing database "cw": Generating default (full) optimizer statistics
vacuumdb: processing database "djexample": Generating default (full) optimizer statistics
vacuumdb: processing database "finance": Generating default (full) optimizer statistics
vacuumdb: processing database "iop": Generating default (full) optimizer statistics
vacuumdb: processing database "learn": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "servicemall": Generating default (full) optimizer statistics
vacuumdb: processing database "store": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics
vacuumdb: processing database "test_store": Generating default (full) optimizer statistics
vacuumdb: processing database "uio": Generating default (full) optimizer statistics
vacuumdb: processing database "vbdev": Generating default (full) optimizer statistics
vacuumdb: processing database "vbdevelop": Generating default (full) optimizer statistics
vacuumdb: processing database "xiuzan": Generating default (full) optimizer statistics
vacuumdb: processing database "xzdevelop": Generating default (full) optimizer statistics

Done
3 重启玩耍
brew services start postgresql

查看下服务状态

brew services list

运行结果

Name           Status  User  Plist
postgresql     started allen /Users/allen/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

接着就可以愉快的使用 pgcli 了。

参考借鉴文章和文档

https://www.postgresql.org/do...

https://stackoverflow.com/que...

http://www.cnblogs.com/space-...

http://www.cnblogs.com/shineq...

http://www.linuxidc.com/Linux...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/38964.html

相关文章

  • PostgreSQL升级方案

    摘要:小版本更新不受影响,比如到升级不受影响。下面介绍两种升级方案可供选择,均是官方文档提及的方案官方文档参考。方案命令是直接对旧的数据库目录文件进行升级的方案,直接将旧版本的数据文件格式升级为新版本使用的格式。 PostgreSQL在默认情况下,是不能跨版本升级的(9.4, 9.5, 9.6等等这些版本跨版本升级。小版本更新不受影响,比如9.6.1到9.6.2升级不受影响)。甚至PG为了数...

    darkerXi 评论0 收藏0
  • Rails 3 升级 Rails 4 中遇到的问题及解决方法

    摘要:的问题当出现不能的时候,我也尝试过这种提示里面的命令,可是还是不能解决这个问题。解决方法一般自己机器上面的都是测试数据,所以可以直接删除掉旧的数据库文件。解决方法然后就可以该干嘛干嘛了。 有些出现的问题其实是不懂正确的流程,都是在试错,可是还是学到了很多东西,写下了,希望对我和大家都有帮助。 Homebrew 的问题 当我去运行brew update的时候出现错误untracked...

    史占广 评论0 收藏0
  • 新书推荐 |《PostgreSQL实战》出版(提供样章下载)

    摘要:作者谭峰张文升出版日期年月页数页定价元本书特色中国开源软件推进联盟分会特聘专家撰写,国内多位开源数据库专家鼎力推荐。张文升中国开源软件推进联盟分会核心成员之一。 很高兴《PostgreSQL实战》一书终于出版,本书大体上系统总结了笔者 PostgreSQL DBA 职业生涯的经验总结,本书的另一位作者张文升拥有丰富的PostgreSQL运维经验,目前就职于探探科技任首席PostgreS...

    Martin91 评论0 收藏0
  • 阿里云FreeBSD系统升级与安装PHP环境

    摘要:所以就安装了另外一个很喜欢的数据库的已经修复了,后面会补上的安装添加自启动初始化数据库启动数据库服务安装上面是一条命令安装自启动配置略安装 系统升级 阿里云的FreeBSD系统默认安装版本是10.1,这个版本已经超出了官方的维护时间了,所以首先要进行系统的版本升级 设置当前系统版本为10.1-release,setenv UNAME_r 10.1-RELEASE 修改update...

    tinna 评论0 收藏0
  • 阿里云FreeBSD系统升级与安装PHP环境

    摘要:所以就安装了另外一个很喜欢的数据库的已经修复了,后面会补上的安装添加自启动初始化数据库启动数据库服务安装上面是一条命令安装自启动配置略安装 系统升级 阿里云的FreeBSD系统默认安装版本是10.1,这个版本已经超出了官方的维护时间了,所以首先要进行系统的版本升级 设置当前系统版本为10.1-release,setenv UNAME_r 10.1-RELEASE 修改update...

    Cobub 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<