参考链接: http://www.jianshu.com/p/354442add14f
- Installed new postgres with Homebrew (started getting the error)
mv /usr/local/var/postgres /usr/local/var/postgres.old
initdb -D /usr/local/var/postgres
pg_upgrade -b /usr/local/Cellar/postgresql/9.0.4/bin -B /usr/local/Cellar/postgresql/9.1.2/bin -d /usr/local/var/postgres.old -D /usr/local/var/postgres
./delete_old_cluster.sh
(this script is created for you automatically in current dir when you go through above steps)rm delete_old_cluster.sh
参考链接: http://serverfault.com/questions/342626/how-do-i-upgrade-postgresl-database-incompatibility-error
连接 PostgreSQL 时报以下错误:
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
打开 PostgreSQL 的服务日志发现是 PostgreSQL 9.5.3 版本升级到 9.6.2 版本后的数据兼容问题:
tail -f /usr/local/var/postgres/server.log
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.3.1.
对于版本的数据升级问题,PostgreSQL 提供了 pg_upgrade 来做版本后的数据迁移,用法如下:
pg_upgrade -b 旧版本的bin目录 -B 新版本的bin目录 -d 旧版本的数据目录 -D 新版本的数据目录 [其他选项...]
数据迁移前,记得先关闭 PostgreSQL 的 postmaster 服务,不然会报以下错误:
There seems to be a postmaster servicing the new cluster.
Please shutdown that postmaster and try again.
Failure, exiting
- 利用 pg_ctl 关闭 postmaster:
pg_ctl -D /usr/local/var/postgres stop
Mac 下也可以这样关闭:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
- 首先备份就版本的数据(默认是在 /usr/local/var/postgres 目录):
mv /usr/local/var/postgres /usr/local/var/postgres.old
- 利用 initdb 命令再初始一个数据库文件:
initdb /usr/local/var/postgres -E utf8 --locale=zh_CN.UTF-8
NOTE:记得加 "--locale=zh_CN.UTF-8" 选项,不然会报以下错误:
lc_collate cluster values do not match: old "zh_CN.UTF-8", new "en_US.UTF-8"
- 最后运行 pg_upgrade 进行数据迁移:
pg_upgrade -b /usr/local/Cellar/postgresql/9.5.2/bin/ -B /usr/local/Cellar/postgresql/9.6.3/bin/ -d /usr/local/var/postgres.old
-D /usr/local/var/postgres -v