其他见:http://my.oschina.net/ensn/blog/636766

本文利用pg_upgrade实现将8.4.18版本升级到9.5.0版本,8.4.18版本为RedHat系统自带pg数据库版本。

环境:Red Hat Enterprise Linux Server release 6.5 (Santiago) X86-64

查看手册看到可以利用pg_upgrade实现从8.4到9.5版本直接升级:

pg_upgrade supports upgrades from 8.3.X and later to the current major release of PostgreSQL, including snapshot and alpha releases.

下载postgresql9.5.0版本:

1.解压:

tar jxvf postgresql-9.5.0.tar.bz2

2.编译:

./configure --prefix=/usr/local/pgsql9.5.0 --with-pgport=5434 --with-wal-blocksize=16   //这里在编译时带with-wal-blocksize=16配置参数导致后面升级出现错误,具体到后面解释。注意指定目录,方便后面管理

***编译过程可能碰到错误:

configure: error: readline library notfound
If you have readline already installed, see config.log for detailson the
failure. It is possible the compiler isnt lookingin the proper directory.
Use --without-readline to disable readlinesupport.

解决方法:

rpm -qa | grep readline
yum search readline

找到符合配置的版本按照:
yum -y install -y readline-deve

安装完毕readline,重新执行上面编译命令。

3.安装:

gmake world
gmake install -world   //此处要加world

4.初始化数据库:

4.1 创建data数据目录:(可另指定其他位置,此处将指定在pg安装目录)

cd /usr/local/pgsql9.5.0
mkdir data

4.2 修改data目录权限,将其赋给postgres:

[root@localhost pgsql9.5.0]# chown -R postgres:postgres /usr/local/pgsql9.5.0/data    
[root@localhost pgsql9.5.0]# chmod -R 700  /usr/local/pgsql9.5.0/data

4.3 初始化:

-bash-4.1$ /usr/local/pgsql9.5.0/bin/initdb -E UTF8 -D /usr/local/pgsql9.5.0/data  --locale=C -U postgres -W   //locale=C这里在升级时报错,后续具体解释
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.
....
初始化过程中会要求设置一个新的密码:postgres
...
Success. You can now start the database server using:

/usr/local/pgsql9.5.0/bin/pg_ctl -D /usr/local/pgsql9.5.0/data -l logfile start

***********************
:/usr/local/pgsql9.5.0/bin/为安装目录,/usr/local/pgsql9.5.0/data为数据目录
:初始化需要在postgres用户下进行,不能再root下
[root@localhost pgsql9.5.0]# /usr/local/pgsql9.5.0/bin/initdb -E UTF8 -D /usr/local/pgsql9.5.0/data  --locale=C -U postgres -W
initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
************************

5.安装pg_upgrade插件:

cd contrib
gamke
gmake install

5.修改postgresql.conf pg_hba.conf文件:

因为升级需要多次连接新老集群数据库实例, 所以修改为使用本地trust认证.
host    all             all             127.0.0.1/32            trust

端口使用注意使用不同的监听端口.(使用编译时候的指定端口:5434)

6.停老库:(新库没有启动,故这里不停)
-bash-4.1$ pg_ctl stop -m fast -D /var/lib/pgsql/data
waiting for server to shut down.... done
server stopped

7.更新:

首先创建一个用户执行升级的目录,该目录权限给postgres用户(执行更新时,会产一些升级文件,创建upgrade目录存储该些文件)
 su - root
 cd /usr/local/pgsql9.5.0
 mkdir upgrade
 chown postgres:postgres upgrade
 su - postgres
 cd /usr/local/pgsql9.5.0/upgrade

7.1检验更新:

-bash-4.1$ /usr/local/pgsql9.5.0/bin/pg_upgrade -c  -b /usr/bin -B /usr/local/pgsql9.5.0/bin -d /var/lib/pgsql/data -D /usr/local/pgsql9.5.0/data -p 5432 -P 5434 -U postgres                             //-c表示检验,-b/-B 表示老/新库bin目录,-d/-D表示老/新库数据目录,具体命令参考手册

检验时碰见以下问题:

1).wal-blocksize 问题

该问题是由前后两个版本wal-blocksize不同,因为之前在安装新版本时指定了wal-blocksize的大小为16k,但老版本是默认安装的,默认大小为8k,该参数大小只能在安装时改变,故需要重新安装pg数据库来解决该问题。

2).lc_collate values for database "postgres" do not match:  old "en_US.UTF-8", new "C" Failure, exiting

删除data数据目录,重新做一次初始化
/usr/local/pgsql9.5.0/bin/initdb -E UTF8 -D /usr/local/pgsql9.5.0/data  --locale=en_US.UTF-8 -U postgres -W

解决以上问题以后重新检验更新:

-bash-4.1$ /usr/local/pgsql9.5.0/bin/pg_upgrade -c  -b /usr/bin -B /usr/local/pgsql9.5.0/bin -d /var/lib/pgsql/data -D /usr/local/pgsql9.5.0/data -p 5432 -P 5434 -U 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 invalid "line" user columns                    ok
Checking for large objects                                  ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok

*Clusters are compatible*

以上说明可以执行更新。

7.2 执行更新

-bash-4.1$ /usr/local/pgsql9.5.0/bin/pg_upgrade -b /usr/bin -B /usr/local/pgsql9.5.0/bin -d /var/lib/pgsql/data -D /usr/local/pgsql9.5.0/data -p 5432 -P 5434

*****

pg_upgrade有两种升级方式,一个是缺省的通过拷贝数据文件到新的data目录下,一个是创建硬链接。拷贝的方式升级较慢,但是原库还可用;硬链接的方式升级较快,但是原库不可用。以上是缺省的方式,硬链接方式升级的命令只需要添加--link

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 invalid "line" user columns                    ok
Checking for large objects                                  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
Setting oldest multixact ID on 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
Setting minmxid counter in new cluster                      ok
Creating newly-required TOAST tables                        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
Checking for large objects                                  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,用来删除旧版本集群数据,当然为了安全起见可以等系统运行几天没问题再来删除。

8.启动新数据库

-bash-4.1$ pg_ctl start -D /usr/local/pgsql9.5.0/data

报错:

server starting
-bash-4.1$ FATAL:  unrecognized configuration parameter "dynamic_shared_memory_type"

解决方法:

原因是因为版本不一致导致,可能是我的主机上面被我编译了两个版本导致的,之前8.4postgresql.conf文件中没有该参数,升级后9.5的配置文件中有该参数,故出现不一致,
通过绝对路径启动数据库:
[root@localhost ~]# su - postgres
-bash-4.1$  /usr/local/pgsql9.5.0/bin/pg_ctl start -D /usr/local/pgsql9.5.0/data
server starting
-bash-4.1$ LOG:  database system was shut down at 2016-03-08 10:55:18 CST
LOG:  MultiXact member wraparound protections are now enabled
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections

进入数据库,此时psql无法进入:

-bash-4.1$ psql
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

通过指定psql路径进入:

-bash-4.1$ /usr/local/pgsql9.5.0/bin/psql  --指定路径
psql (9.5.0)
Type "help" for help.

postgres=#

9.执行收集统计信息的脚本:
-bash-4.1$ /usr/local/pgsql9.5.0/upgrade/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/pgsql9.5.0/bin/vacuumdb" --all --analyze-only

vacuumdb: processing database "gedbs": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "gedbs": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "gedbs": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

Done

10.修改bash_profile文件中的PGDATA, PGPORT等参数(对postgres用户有效,客户端用其他用户登陆可能无效,修改/etc/profile对所有用户有效),修改PATH搜索路径,
不然在执行psql是会报版本不一致警告:

WARNING: psql version 8.4, server version 9.5.

11.启动数据库,pgadmin连接

报错:configure FATAL:  password authentication failed for user "postgres"

解决方法:

修改postgres用户密码,ALTER USER postgres PASSWORD 'newPassword';

参考:http://stackoverflow.com/questions/7695962/postgresql-password-authentication-failed-for-user-postgres

12.新数据库稳定后,删除老版本软件
[root@localhost upgrade]# cat delete_old_cluster.sh
#!/bin/sh

rm -rf '/var/lib/pgsql/data'

$ ./delete_old_cluster.sh

参考:

http://www.postgresql.org/docs/current/static/pgupgrade.html

http://my.oschina.net/liuyuanyuangogo/blog/500229
http://xmarker.blog.163.com/blog/static/22648405720145203428811/
http://906179271.iteye.com/blog/2263218
http://francs3.blog.163.com/blog/static/40576727201519521050/
http://wangwei.info/upgrade-postgresql-centos/

postgresql利用pg_upgrade升级数据库(从8.4升级到9.5)的更多相关文章

  1. Windows版 PostgreSQL 利用 pg_upgrade 进行大版升级操作

    最近 PostgreSQL 15 版本正式发布了,新版本的各种特性和好处本文就不展开介绍了,主要介绍一下 Windows 环境下 PostgreSQL 大版本升级的方法,我们现在的几个数据库都是运行在 ...

  2. 【PM】关于系统数据库和服务现场升级的一些看法

    工作快满一年了,立即着手准备第二次出差去升级我们的系统,可是突然想到一件事情,让我颇有感触,是关于系统现场升级的. 我们迭代开发的系统隔一段时间就会须要到用户的现场去为其进行系统升级,当中升级包含cl ...

  3. 【Oracle】实现Oracle数据库对象的一键升级

    引言     公司内部的项目比较倾向于将业务逻辑放在oracle存储过程中实现,所以每次项目升级都涉及到很多的oracle表,存储过程等数据库对象的升级.然而采取的升级方式是比较"原始&qu ...

  4. 打造基于 PostgreSQL/openGauss 的分布式数据库解决方案

    在 MySQL ShardingSphere-Proxy 逐渐成熟并被广泛采用的同时,ShardingSphere 团队也在 PostgreSQL ShardingSphere-Proxy 上持续发力 ...

  5. 利用SQl对数据库实行数据拆分与组合

    利用SQl对数据库实行数据拆分与组合实现提供以下几种方案: 方法一: WITH CTE AS (SELECT A.Id,A.[Uid],UserName FROM (SELECT A.[id], RE ...

  6. Oracle_RAC数据库GI的PSU升级(11.2.0.4.0到11.2.0.4.8)

    Oracle_RAC数据库GI的PSU升级(11.2.0.4.0到11.2.0.4.8) 本次演示为升级oracle rac数据库,用GI的psu升级,从11.2.0.4.0升级到11.2.0.4.8 ...

  7. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  8. Android入门(十)SQLite创建升级数据库

    原文链接:http://www.orlion.ga/603/ 一.创建数据库 Android为了让我们能够更加方便地管理数据库,专门提供了一个 SQLiteOpenHelper帮助类, 借助这个类就可 ...

  9. Android学习笔记(十八)——再谈升级数据库

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 之前我们为了保证数据库中的表是最新的,只是简单地在 onUpgrade()方法中删除掉了当前所有的表,然后强制 ...

随机推荐

  1. localhost与127.0.0.1的区别

    localhost与127.0.0.1的区别是什么 定义 localhost也叫local ,正确的解释是:本地服务 127.0.0.1在windows等系统的正确解释是:本机地址(本机服务器) 不同 ...

  2. amCharts图表中的JavaScript中文注释引起的浏览器兼容性问题

    近期用amCharts做图表.一切都很顺利,然后演示的时候掉链子了,平时开发的时候都是用的火狐和谷歌,加上这种图表框架本来就号称兼容性极好,也没有在ie上测试,演示的机器上恰巧用的是ie11,发现一个 ...

  3. js parseInt和map函数

    今天看了一个js的题目["1","2","3"].map(parseInt),看到后脑海中浮现的答案是[1,2,3],但是看到正确答案后蒙了 ...

  4. 基于ionic+cordova+angularJs从零开始搭建自己的移动端H5 APP

    这里详细介绍下如何用ionic+cordova+angularjs搭建自己的移动端app,包括环境搭建,框架使用等,具体项目已放置在github上,可下载下来自行启动. 下载地址:https://gi ...

  5. Struts2 easy UI插件

    一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...

  6. PAT 1035. 插入与归并(25)

    根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...

  7. C# 窗体缩放的时候同步改变控件的大小和字体

    最新在写个小程序,需要窗体填满各种尺寸的显示器,同时需要同步缩放控件的大小.于是就写了个类,简单的调用一下即可解决问题. 这个类可以同步缩放控件的位置,宽度高度,字体大小. 使用的时候在FormLoa ...

  8. [[iso教程]] 《4个月ios实体教程》全网最新、最全ios视频教程

    全网最新.最全ios视频教程 内容简介 <ios实体教程>主要介绍如何使用iOS提供的强大工具集创建iOS应用.全视频对iOS操作系统做了全面的介绍,首先讲解如何构建应用程序的用户界面,涵 ...

  9. CSS中不为人知Zoom属性的使用介绍(IE私有属性)

    其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支持.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小作用,比如触发ie的hasLayout属性,清除浮动.清除margin的 ...

  10. Switch&NAT 测试

    测试环境: PC1:Windows10 iperf3 PC2:Ubuntu iperf3 都装有千兆网卡,直连的iperf速度是935Mbps. 因为TXRX两个方向的数据是差不多的,下面的测试数据只 ...