How to install PostgreSQL on OSX Yosemite using MacPorts

I want to installPostgreSQLfor a node project that I'm developing inOSX Yosemite. I use MacPorts and so tried the method described here:https://github.com/codeforamerica/ohana-api/wiki/Installing-PostgreSQL-with-MacPorts-on-OS-X

...but I get an error during step 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.

...thinking that I may not need to install the pg gem since I want to work with Node and not Ruby, I moved on to the next steps. But there I run into an error during step 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.

...checking my /opt/local/lib/postgresql93/bin/ directory, I see bothinitdbandpostgres. I see those lines sayingPermission deniedand am wondering what that's about.

Not sure how to progress. Thinking of using Postgres.app if it really is easier but not sure whether it would be better to install using MacPorts since I install most other things using MacPorts. Tips about any of my problems are appreciated!

The permissions/ownership on the directories between/anddefaultdblikely need to be fixed. I think PostgreSQL can be sensitive to ownership of these, though it seems that in your case PostgreSQL simply doesn't have access to those. This is what I have for each directory.

$ 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

You could fix permissions by doingsudo chmod a+rx /opt/local/var/db/as needed.

For thedefaultdbdirectory itself, you should follow the instructions that you link to, which seem to have the same as I have:

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

Below are instructions adapted from myblog(though I recommend using PostgreSQL 9.4, which I now do). I've been running PostgreSQL using MacPorts since 9.1 without major problems.

1. Install PostgreSQL using MacPorts.

Of course, I assume you’ve got MacPorts up and running on your system.

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

2. Set up PostgreSQL

I first need to initialize the database cluster and then get the server running. The following comes straight from the on-screen instructions supplied with the MacPorts portpostgresql93-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'

Note that MacPorts creates a launch daemon. To load it now and to make sure it launches on system start, do:

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

I then usepsqlfor some set-up to getmydatabase going.

sudo su - postgres
/opt/local/lib/postgresql93/bin/psql -U postgres -d template1

If you get to here, then you have PostgreSQL running on your system.

I had the same issue when attempting to runinitdb, even when following the description by 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.

Turns out that userpostgrescannot do anything if you try to make it run a command from within your own home directory, because in therepostgresis not allowed to read its own location and hence cannot figure out any other paths, either. So the simple solution is to runcd /before any command that must be run aspostgres(initdb,pg_ctl, etcetera). Afterwards, you can quickly jump back to your previous working directory usingcd -.

What Others Are Reading