【问题标题】:how to fix postgres after updating/upgrading brew如何在更新/升级 brew 后修复 postgres
【发布时间】:2014-01-12 07:53:40
【问题描述】:

我升级到 mavericks 并且在安装/编译新 gem 时遇到了一些问题,所以我重新安装了 xcode 并进行了 brew 更新和升级。 Gems 现在可以工作,甚至 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.

我现在应该怎么做才能让我的数据库再次工作?

【问题讨论】:

    标签: postgresql homebrew


    【解决方案1】:
    $ 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 表示您的编辑评论。此外,如果您错误地升级并需要恢复,您真正需要的是:rm -rf /usr/local/Cellar/postgresql/9.4... 后跟 brew switch postgresql 9.4。 ...
    • 这对 2016 年有效吗?
    • 它似乎可以在 9.2 和 9.3 以及 9.3 和 9.4 之间架起桥梁...你为什么不尝试备份你的数据文件,试试这个过程,然后告诉我们它是如何实现的结果?
    • brew install postgresql92,没有名称为“postgresql92”的可用公式
    • 看起来 postgresql92 已从 brew 中删除。您愿意尝试 93 或 94 并在这里报告吗? github.com/Homebrew/homebrew-versions
    【解决方案2】:

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

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

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

    那么您应该使用pg_dump 或pg_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
    

    【讨论】:

      【解决方案3】:

      对于 9.2 到 9.5,在 El Capitan 上,我遇到了一些可能对其他人有帮助的问题。

      现在看来,switch 语句中使用的包名称已更改为包含版本号(postgresql92 与 postgresql)。因此,我需要添加取消链接当前版本的 postgres 的步骤:

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

      为了执行转储,我需要启动 postgres 服务器,

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

      但后来我遇到了可怕的:

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

      我查看了日志,发现了这个:

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

      根据Donovan 的this answer,Yosemite 和El Capitan 删除了一些需要的目录。所以我还需要使用Nate这个很棒的命令将这些目录添加回来

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

      所以完整的更新版本是:

      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
      

      所有功劳归于sentinel、Nate 和 Donovan。非常感谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-02
        • 1970-01-01
        • 1970-01-01
        • 2020-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多