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



我想为正在OSX Yosemite中开发的节点项目安装PostgreSQL。我使用MacPorts,因此尝试了此处描述的方法:https://github.com/codeforamerica/ohana-api/wiki/Installing-PostgreSQL-with-MacPorts-on-OS-X

但我在第二步遇到了一个错误:

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

考虑到我可能不需要安装pggem,因为我想使用Node而不是Ruby,我继续下一步。但在步骤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来安装大多数其他东西。关于我的任何问题的提示都将不胜感激!

可能需要修复/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会创建一个启动守护程序。要立即加载并确保它在系统启动时启动,请执行:

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。

我在尝试运行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不允许读取自己的位置,因此也无法找到任何其他路径。因此,简单的解决方案是在任何必须作为postgresinitdbpg_ctl等)运行的命令之前运行cd /。之后,您可以使用cd -快速跳回到以前的工作目录。

最新更新