更新/升级brew后如何修复postgres

时间:2013-12-24 03:58:53

标签: postgresql homebrew

我升级为mavericks并且在安装/编译新宝石时遇到了一些问题,所以我重新安装了xcode并进行了brew更新和升级。宝石现在工作,甚至postgres继续工作一段时间,直到最近重新启动。现在,postgres似乎遇到了问题。

postgres:

postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.


brew info postgres:

postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.2.4 (2842 files, 39M)
  Built from source
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 39M) *
  Poured from bottle

postgres -D /usr/local/var/postgres:

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.2.

我现在该怎么做让我的数据库再次运行?

3 个答案:

答案 0 :(得分:26)

$ brew tap homebrew/versions
$ brew install postgresql92
$ brew switch postgresql 9.2.8 # might need to check this one, could be newer by the time you read this
$ pg_dumpall > ninedottwo-dump
$ brew uninstall postgresql92
$ brew switch postgresql 9.3.4 # again, check version
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
$ createdb # i got an error about my database not existing, so i had to run this
$ psql < ninedottwo-dump
$ bundle exec rails s

感谢彼得让我开始朝着正确的方向前进。

编辑:这将教会我一次升级所有内容......

答案 1 :(得分:5)

您需要以某种方式获得PostgreSQL 9.2的副本,才能访问现有的数据目录。

通过Homebrew安装9.2的选项包括:

  • postgresql公式升级到9.3之前,先查看Homebrew。
  • postgresql92点按
  • 安装homebrew/versions
  • postgresql-9.2点击安装petere/postgresql(披露:这是我的项目)。
  • 从源代码安装。

然后你应该使用pg_dumppg_upgrade将其升级到9.3。

pg_upgrade就是出于这个目的,使用它很简单。只需确保为旧数据和二进制文件指定正确的路径(-d和-b)以及指向新数据和二进制文件的正确路径(-D和-B):

pg_upgrade \
-d /usr/local/var/postgres/9.2/ -D /usr/local/var/postgres/9.3/ \
-b /usr/local/Cellar/postgresql/9.2.4/bin/ -B /usr/local/Cellar/postgresql/9.3.2/bin/

如果一切顺利,您可以使用brew cleanup postgresql清除旧版本, 由于新版本的PostgreSQL意味着Ruby pg gem使用的本机库的新版本,您可能需要重新编译它:

gem uninstall pg
ARCHFLAGS="-arch x86_64" gem install pg

答案 2 :(得分:2)

For 9.2 to 9.5, on El Capitan, I ran into a couple problems which might help others.

It seems now the name of the package used in the switch statement has changed to include the version number (postgresql92 vs postgresql). So, I needed to have the added step of unlinking the current version of postgres:

brew unlink postgresql
brew link postgresql92
brew switch postgresql92 9.2.13 # match version

In order to perform the dump I needed to start the postgres server,

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

But then I ran into the dreaded:

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"?

I check out the logs and found this:

could not open directory "pg_tblspc": No such file or directory

According to this answer by Donovan, Yosemite and El Capitan remove some needed directories. So I also needed to add those directories back in using this awesome command by Nate

mkdir /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/

So the full updated version is:

brew tap homebrew/versions
brew install postgresql92
brew unlink postgresql
brew switch postgresql92 9.2.13 # might need to check this one, could be newer by the time you read this
mkdir /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
pg_dumpall > ninedottwo-dump
pg_ctl -D /usr/local/var/postgres stop
brew unlink postgresql92
brew uninstall postgresql92
brew switch postgresql 9.5.2 # again, check version
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
createdb # i got an error about my database not existing, so i had to run this
psql < ninedottwo-dump
bundle install # need to make sure you have pg gem for 9.5
bundle exec rails s

All credit goes to sentinel, Nate, and Donovan. Thanks so much!