【问题标题】:Homebrew postgres broken自制的 postgres 坏了
【发布时间】:2015-02-26 07:57:27
【问题描述】:

我使用自制软件在我的 Mac (10.10.1/Yosemite) 上安装了 Postgresql 9.4.0。它不起作用。

我在 ~/Library/LaunchAgents 中创建了指向 /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist 的软链接。

如果我尝试手动加载 postgres,我会收到“操作正在进行中”的消息

> launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/9.4.0/homebrew.mxcl.postgresql.plist: Operation already in progress

但是 postgres 似乎没有运行。

> ps auxw | grep post
billmcn           670   0.0  0.0  2424272    452 s000  R+   10:12PM   0:00.01 grep post

我无法连接命令行客户端。

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

据我所知,我已经尝试了在讨论此问题的 other Stackoverflow 线程上建议的所有修复。具体来说:

  • 我已经卸载并重新安装了 postgres 和随附的 Ruby gem。我的机器上没有 postgres 8.0 版本。
  • 我已验证 psql 客户端程序是 Homebrew 安装的 9.4.0 版本,而不是 Mac 系统二进制文件。
  • 我已验证 /usr/local/var/postgres/postmaster.pid 不存在。
  • 我已经重启了机器。

我之前确实有 Homebrew postgres 在这台机器上工作。我认为破坏它的原因是从版本 8 升级到版本 9,但我不确定。

我没有任何需要保留的数据库。我愿意从 postgres 开始;我现在只需要让它工作。有什么想法吗?


问题似乎出在 /usr/local/var/postgres 目录的权限上。这是我的 var 目录在事情不工作时的样子。

ll /usr/local/var/
drwxr-xr-x  3 billmcn  admin  102 Dec 20 12:44 cache
drwxr--r--  2 root     admin   68 Dec 29 21:37 postgres

(whoami = "billmcn")

我删除了/usr/local/var/postgres,卸载重装了postgres,现在是这个样子。

ll /usr/local/var/
drwxr-xr-x   3 billmcn  admin  102 Dec 20 12:44 cache
drwx------  23 billmcn  admin  782 Dec 30 10:51 postgres

不知道它是如何进入这种状态的,因为我不记得在这个目录的权限上做了什么,但没关系。现在可以了。

【问题讨论】:

    标签: macos postgresql homebrew


    【解决方案1】:

    我在新安装的 Yosemite 上使用自制软件安装 postgres 时遇到了同样的问题。

    首先我的 brew 配置看起来像这样:

    HOMEBREW_VERSION: 0.9.5
    ORIGIN: https://github.com/Homebrew/homebrew
    HEAD: 9f6926265f8e4be7cc80dfe9042f2cd3c1e8dc9e
    Last commit: 64 minutes ago
    HOMEBREW_PREFIX: /usr/local
    HOMEBREW_CELLAR: /usr/local/Cellar
    CPU: quad-core 64-bit sandybridge
    OS X: 10.10.1-x86_64
    Xcode: 6.1.1
    Clang: 6.0 build 600
    X11: N/A
    System Ruby: 2.0.0-481
    Perl: /usr/bin/perl
    Python: /usr/bin/python
    Ruby: ~/.rvm/rubies/ruby-2.1.1/bin/ruby
    

    我注意到的第一件事是我没有对/usr/local/var/postgres 的写权限。这很容易改变发出sudo chown -R `whoami` /usr/local/var/postgres 然后我重新安装了 postgresql 并做了

    cat /usr/local/var/postgres/server.log

    其中透露:

    postgres cannot access the server configuration file "/usr/local/var/postgres/postgresql.conf": No such file or directory

    所以我删除了目录/usr/local/var/postgres 并发出了初始化数据库的命令。

    initdb -D /usr/local/var/postgres/

    这似乎成功了,postgres 运行良好。

    【讨论】:

    • 我遵循了这些步骤,但在执行 initdb 命令后,我得到:“无法访问目录“/usr/local/var/postgres”:权限被拒绝。”同样,这是在您列出的步骤之后,包括在运行 initd 之前直接删除该目录。
    • 系统通常不会依赖这些东西。如果您执行“ls -l /usr/local/var | grep postgres”,您的用户是否设置为目录的所有者?
    • 在看到您的评论之前,我刚刚放弃并切换到 macports。而且,它现在工作得很好。
    • 天哪!非常感谢。自2小时以来,我一直在为此苦苦挣扎。我认为 Homebrew 应该简化了安装。
    • 我希望我能 +5 这个。
    【解决方案2】:

    我遇到了同样的问题。这里的主要问题是安装的 initdb 步骤将创建具有 root 所有权的目录,而不是作为 Mac 上的用户。要解决这个问题:

    运行initdb之前创建数据目录并设置权限为0700

    rm -rf /usr/local/var/postgres  # in case this is not your first try
    mkdir /usr/local/var/postgres
    chmod 0700 /usr/local/var/postgres
    

    然后运行initdb,它将尊重数据目录的权限。

    initdb -D /usr/local/var/postgres
    

    为了笑容和笑声,创建一个以您的用户命名的测试数据库:

    createdb `whoami`
    

    登录测试:

    psql
    

    【讨论】:

    • 我去rm -rf /usr/local/var/postgres 和或sudo rm -rf /usr/local/var/postgres 似乎都删除了postgres 目录,然后我去手动添加我自己的新目录,我得到mkdir: /usr/local/var/postgres: File exists !? psql 仍然说 no such file or directory。我的情况背景:stackoverflow.com/questions/31088325/…
    【解决方案3】:

    在尝试使用 Homebrew 安装 postgresql 后,我得到了这个:

    Warning: postgresql-9.5.2 already installed, it's just not linked
    

    所以我尝试了:

    brew link postgresql
    

    得到了这个错误:

    Linking /usr/local/Cellar/postgresql/9.5.2... 
    Error: Could not symlink share/man/man3/SPI_connect.3
    /usr/local/share/man/man3 is not writable.
    

    这似乎是一个写权限的问题,所以我做了:

    sudo chown -R `whoami` /usr/local/share/man/
    

    它成功了,因为,然后我能够做到(没有错误):

    brew link postgresql
    

    【讨论】:

      【解决方案4】:

      请注意,他们是 Homebrew 的 github 上处理此问题的线程:https://github.com/Homebrew/homebrew/issues/35240


      我也遇到过类似的问题。詹姆斯的回答帮助我解决了这个问题。但后来我遇到了 jbk 提到的问题(在删除 /usr/local/var/postgres 后,它继续被重新创建)。

      问题是,如果您创建了符号链接:

      ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
      

      并启动进程:

      launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
      

      你应该先卸载它:

      launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
      

      在运行 James 的命令之前。

      rm -rf /usr/local/var/postgres  # in case this is not your first try
      mkdir /usr/local/var/postgres
      chmod 0700 /usr/local/var/postgres
      

      此外,如果像我一样,您有一个管理自制软件的管理员用户和一个将使用 pgsl 进行开发的普通用户,那么应该以超级用户身份运行 James 命令:

      sudo -s
      

      并且应该将 postgres 目录的所有权授予您的开发用户:

      chown my-dev-user /usr/local/var/postgres
      

      以下命令以 dev 用户身份运行,然后应正确填充目录:

      createdb `whoami`
      

      跑步:

      psql -l
      

      应该在 postgre 中显示这些操作后的表和用户权限。

      希望这会有所帮助。

      【讨论】:

      • 这太棒了!谢谢塞德里克。我在权限方面遇到了同样的问题。
      【解决方案5】:

      如果有人从以前的版本升级,不要忘记:

      brew postgresql-upgrade-database
      

      这将通过将现有数据库升级到您将 postgres 升级到的版本来解决问题。

      【讨论】:

      • 这为我修好了。
      【解决方案6】:

      为了后代,我遇到了这个问题,并想说明什么对我有用。

      我在 High Sierra 上运行 postgres 11.2。我最近使用brew postgresql-upgrade-database 从 postgres 10 升级。

      我不断收到错误psql: could not connect to server: No such file or directory,我的server.log 显示is another postmaster (PID 5894) running in data directory "/usr/local/var/postgres"?

      我尝试了多种解决方案,包括重新启动计算机、删除postmaster.pid、使用brew services restart postgres,但均无济于事。我最终偶然发现了解决方案:

      brew unlink postgresql && brew link postgresql

      不知道为什么会这样,但主要是把它放在这里,以便我以后自己参考!往墙上扔东西直到它粘住!

      【讨论】:

        【解决方案7】:

        我最近遇到了一个问题,这始于我升级了一些 brew 更新/升级,主要是 python 版本等。什么对我有用。

        brew uninstall postgres
        brew install postgresql@9.5
        echo 'export PATH="/usr/local/opt/postgresql@9.5/bin:$PATH"' > ~/.zshrc
        # you may need > ~/.bashrc if you use bash
        

        我需要 pg_dump、pg_restore 等才能让我做的工作

        brew install libpq
        

        启动服务

        brew services start postgresql@9.5
        

        从这里我本来希望一切正常,但所有 rails db 命令仍然给出服务器未运行的错误。最后一点是最后为我解决的难题。

        gem uninstall pg
        gem install pg -v 0.20.0 # which was set in Gemfile
        # could also just probably do bundle install instead.
        

        【讨论】:

          【解决方案8】:

          检查@leo_chaz_maltrait 以修复错误Could not symlink share/man/man3/SPI_connect.3 另一个可能出现的错误是:

          Error: Could not symlink lib/pkgconfig/libecpg.pc

          sudo chown -R `whoami` /usr/local/lib/pkgconfig
          
          brew link postgresql
          

          【讨论】:

            【解决方案9】:

            在日志中看到此文件后,我不得不删除 .pid 文件

            /usr/local/var/log/postgres.log

            2021-10-10 19:05:27.468 BST [41868] FATAL:  lock file "postmaster.pid" already exists
            2021-10-10 19:05:27.468 BST [41868] HINT:  Is another postmaster (PID 820) running in data directory "/usr/local/var/postgres"?
            
            
            rm /usr/local/var/postgres/postmaster.pid
            

            我使用 brew 安装了它

            【讨论】:

              【解决方案10】:

              基于@James 的回答,这是我在 M1 Monterey 机器上所做的。对我来说,目录不同。

              在终端:

              #to fix postgresql of existing installation
              cd /opt/homebrew/var
              rm -rf postgres
              mkdir postgres
              chmod 0700 postgres
              initdb -D postgres
              
              #install postgres
              echo "installing postgres..."
              brew install postgresql
              brew services restart postgresql
              createuser postgres -s
              

              然后我可以brew install --cask pgadmin4 并从应用程序运行 pgadmin 并连接到 127.0.0.1。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-11-12
                • 2014-05-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-09-29
                相关资源
                最近更新 更多