将PostgreSQL 9.4升级到PostgreSQL 9.5的方法是使用pg_upgrade
在开发环境中执行了brew update; brew upgrade之后,由于最新版本的PostgreSQL9.5.0刚刚发布,因此使用pg_upgrade来迁移数据库内容。
准备工作
查询通过brew安装的PostgreSQL9.4和9.5的安装路径。
-
- 9.4が /usr/local/Cellar/postgresql/9.4.5_2
9.5が /usr/local/Cellar/postgresql/9.5.0
如果这样的话。
另外,
-
- これまでのPostgreSQL9.4のPGDATAフォルダ(古いPGDATA)のパスを/database/data94
新しく作るPostgreSQL9.5のPGDATAフォルダ(新しいPGDATA)のパスを/database/data95
PostgreSQLの管理ユーザ をfoo(特に指定していなければログインしているユーザのはず)
假设这样说。
為了安全起見,將PostgreSQL 9.4的符號連接切換至9.5。
理论上,应该在“brew upgrade”进行升级时自动切换,但为了保险起见,我们需要确认一下。
psql --version
# psql (PostgreSQL) 9.5.0 と表示
# もし9.4のままの場合は、brew switchなどでシンボリックリンクを修正
# ex) brew switch postgresql 9.5.0
为了安全起见,停止PostgreSQL 9.4。
/usr/local/Cellar/postgresql/9.4.5_2/bin/pg_ctl stop
创建并初始化一个新的PGDATA文件夹。
使用PostgreSQL 9.5来初始化initdb。这时候,
请使用与旧集群兼容的 initdb 选项。
请注意,根据文件中的记录(即在9.4版本时通过initdb命令创建PGDATA文件夹时使用的相同选项),在本文中使用–no-locale -E UNICODE -U foo(无区域设置,默认编码为UTF8,数据库超级用户为foo)。
mkdir /database/data95
initdb -D /database/data95 --no-locale -E UNICODE -U foo
将环境变量$PGDATA的路径从旧的PGDATA文件夹切换到新的PGDATA文件夹。
echo $PGDATA
# /database/data94 と表示される
export PGDATA="/database/data95"
# .bash_profileなどで初期設定している場合は、そちらも変更
执行pg_upgrade
使用pg_upgrade命令,将旧的PGDATA文件夹的内容同步到新的PGDATA文件夹中。
pg_upgrade -d /database/data94 -D /database/data95 -b /usr/local/Cellar/postgresql/9.4.5_2/bin -B /usr/local/Cellar/postgresql/9.5.0/bin -U foo
运行示例
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
Checking database user is a superuser 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
Creating dump of global objects ok
Creating dump of database schemas
ok
Checking for presence of required libraries ok
Checking database user is a superuser 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
Adding support functions to new cluster ok
Restoring database schemas in the new cluster
ok
Creating newly-required TOAST tables ok
Removing support functions from 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
只要没有出现错误或中断的日志,就算是成功了。
最后,执行pg_upgrade生成的analyze_new_cluster.sh并结束。
pg_ctl start
analyze_new_cluster.sh
psql -l
# 9.4のデータベースの内容が移行されていることを確認
另外,pg_upgrade还会生成一个名为delete_old_cluster.sh的脚本,用于删除旧的PGDATA文件夹(在此示例中为/database/data94)。您可以选择执行此脚本以删除旧的PGDATA文件夹,或者根据个人喜好选择保留它们。
如果需要调整,由于pg_hda.conf和postgresql.conf不会迁移,您需要自行进行修正。