如何使用MacPorts在OSX Yosemite上安装PostgreSQL

6
我想在我的OSX Yosemite开发的node项目中安装PostgreSQL。我使用MacPorts,并尝试了这里描述的方法:https://github.com/codeforamerica/ohana-api/wiki/Installing-PostgreSQL-with-MacPorts-on-OS-X,但是在第2步中会出现错误:
$ sudo gem install pg -- --with-pg-config=/opt/local/lib/postgresql93/bin/pg_config > ruby_error
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-pg-config=/opt/local/lib/postgresql93/bin/pg_config
Using config values from /opt/local/lib/postgresql93/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

我曾想着既然是要使用 Node 而不是 Ruby,也许就不需要安装 pg gem,于是跳过了安装过程,进行接下来的步骤。但在第 3.3 步时遇到了错误:

$ sudo su postgres -c '/opt/local/lib/postgresql93/bin/initdb -D /opt/local/var/db/postgresql93/defaultdb'
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "postgres" is needed by initdb but was not found in the
same directory as "initdb".
Check your installation.

检查我的 /opt/local/lib/postgresql93/bin/ 目录,我看到了 initdbpostgres。我看到那些显示 "Permission denied" 的行并想知道这是怎么回事。

不确定如何继续。考虑使用 Postgres.app 如果它真的更容易,但不确定是否最好使用 MacPorts 进行安装,因为我大多数其他东西都使用 MacPorts 进行安装。感激任何关于我的问题的建议!


你似乎比指南中的sudo更频繁地使用它,有什么原因吗?编辑:另外,我们能看到mkmf.log吗? - Tom
我最终安装了PostgresApp,但我认为你说得对,我使用sudo的次数比必要的多。一旦我回到这个项目,我会带着你请求的信息回来。 - Miguel Valencia
2个回答

14

/defaultdb之间的目录的权限/所有权可能需要修复。我认为PostgreSQL对这些目录的所有权很敏感,尽管在您的情况下,PostgreSQL似乎只是无法访问这些目录。这是每个目录的情况。

$ ls -hlt /opt/local/var/db/
total 0
drwxr-xr-x  7 root  admin   238B Jan 23 16:54 texmf
drwxr-xr-x  3 root  admin   102B Dec 25 07:37 postgresql94

如果需要,您可以通过执行 sudo chmod a+rx /opt/local/var/db/ 来修复权限。

对于defaultdb目录本身,您应该遵循所链接的说明,这似乎与我拥有的相同:

sudo chown postgres:postgres /opt/local/var/db/postgresql93/defaultdb

以下是我从我的博客中改编而来的说明(虽然我现在建议使用PostgreSQL 9.4)。自从9.1以来,我一直在使用MacPorts运行PostgreSQL而没有遇到任何大问题。

1. 使用MacPorts安装PostgreSQL。

当然,我假设您已经在系统上安装并运行了MacPorts。

sudo port install postgresql93 +perl +python27
sudo port install postgresql93-server

2. 设置PostgreSQL

我首先需要初始化数据库集群,然后启动服务器。以下内容直接来自于MacPorts端口postgresql93-server所提供的屏幕指令。

sudo mkdir -p /opt/local/var/db/postgresql93/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql93/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql93/bin/initdb -D /opt/local/var/db/postgresql93/defaultdb'
请注意,MacPorts创建了一个启动守护进程。要立即加载它并确保它在系统启动时启动,请执行以下操作:

请注意,MacPorts创建了一个启动守护进程。要立即加载它并确保它在系统启动时启动,请执行以下操作:

sudo defaults write /Library/LaunchDaemons/org.macports.postgresql93-server.plist Disabled -bool false
sudo launchctl load /Library/LaunchDaemons/org.macports.postgresql93-server.plist

我随后使用psql进行设置,以启动我的数据库。

sudo su - postgres
/opt/local/lib/postgresql93/bin/psql -U postgres -d template1
如果您能在此处到达,则说明您的系统上正在运行PostgreSQL。

6

当我尝试运行initdb时,遇到了与Ian Gow描述相同的问题:

$ sudo su postgres -c '/opt/local/lib/postgresql94/bin/initdb -D /opt/local/var/db/postgresql94/defaultdb'
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "postgres" is needed by initdb but was not found in the
same directory as "initdb".
Check your installation.

原来如果你尝试让用户postgres在自己的家目录中运行命令,它将无法执行任何操作,因为在那里postgres不允许读取自己的位置,因此也无法找到任何其他路径。所以简单的解决方法是在必须作为postgres运行的任何命令之前运行cd / (例如initdbpg_ctl等),然后可以使用cd - 快速回到先前工作的目录。

这对我来说非常简单。在重置后,我尝试重新设置配置文件管理器。cd /是我需要的技巧。谢谢。我距离重建整个Mac服务器只有几分钟的时间。 - portalpie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接