PostgreSQL 升级版本从11.2变为12.4

当我升级Ruby版本时使用”brew upgrade”命令,PostgreSQL自动更新了。结果导致无法连接到Postgres,让我感到焦急。由于疏忽了更新时的提示信息,修复花费了一些时间,因此我将此记录下来。

升级 brew 时的提示信息。

我没有注意到这里有处理方法,就关闭了终端,导致后来很困扰。

# Ruby のバージョンをアップデートしようとした
$ brew upgrade rbenv ruby-build
...
# 自動的に PostgreSQL もアップデートしてしまう
==> Installing postgresql
==> Pouring postgresql-12.4.mojave.bottle.tar.gz
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database <- ここに対処法

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/12/app-initdb.html

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
?  /usr/local/Cellar/postgresql/12.4: 3,223 files, 37.4MB
Removing: /usr/local/Cellar/postgresql/11.2... (3,186 files, 35.4MB)
==> Upgrading python@3.8 3.8.6 -> 3.8.6 
...

PostgreSQL 无法启动。

当我想要像往常一样开始工作时,数据库无法启动,让我非常着急…

$ pg_ctl -D /usr/local/var/postgres start
waiting for server to start....2020-10-17 13:26:58.628 JST [57774] FATAL:  database files are incompatible with server
2020-10-17 13:26:58.628 JST [57774] DETAIL:  The data directory was initialized by PostgreSQL version 11, which is not compatible with this version 12.4.
 stopped waiting
pg_ctl: could not start server
Examine the log output.

(处理方法)PostgreSQL 更新后的操作。

我将执行之前消息中所写的代码。

$ brew postgresql-upgrade-database

将.bash_profile文件中的设置重新编写

$ vim ~/.bash_profile

# postgres
export PATH=/usr/local/Cellar/postgresql/12.4/bin/:$PATH
export PGDATA=/usr/local/var/postgres

$ source ./~bash_profile

再次尝试连接时,成功了!

$ pg_ctl -D /usr/local/var/postgres start
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
广告
将在 10 秒后关闭
bannerAds