Install PL/R on OS X 10.9 using MacPorts

In an earlier post, I detailed how to get PL/R up and running on Mac OS X. Recently, I’ve had reason to want PL/Python on my system too, but I have not been able to get it going using my self-compiled version of PostgreSQL. It seems to compile OK, but each time I tried CREATE EXTENSION plpython2u, the connection crashed. I suspect that the issue was related to differences in how the versions of (MacPorts-supplied) Python and PostgreSQL I had were compiled.

After toying with the idea of switching back to Ubuntu/Linux Mint (which inevitably means running two operating systems, as Linux doesn’t have everything I need), I thought I would give the MacPorts version of PostgreSQL a spin. However, the first time I tried, I could not get PL/R to compile. The second time I tried the +perl +python27 version of the port postgresql95 and it worked fine. I think this version differs in that it is compiled locally—rather than just being a binary like the standard version—and presumably is something like the postgresql-server-dev-9.5 package available for Ubuntu.

Short version:
1. Installed PostgreSQL using MacPorts.

2. Compiled PL/R.

3. Did the usual admin stuff to get PostgreSQL up and running and CREATE EXTENSION plr;

4. Restored my database from a backup.

Long version:

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

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

2. Install PL/R. Note that this is exactly as I’ve done before, but using a different version of pg_config.

cd ~
wget http://www.joeconway.com/plr/plr-8.3.0.16.tar.gz
tar xzf plr-8.3.0.16.tar.gz
cd plr
R_HOME="/Library/Frameworks/R.framework/Resources" USE_PGXS=1 PG_CONFIG="/opt/local/lib/postgresql95/bin/pg_config"  make
sudo R_HOME="/Library/Frameworks/R.framework/Resources" USE_PGXS=1 PG_CONFIG="/opt/local/lib/postgresql95/bin/pg_config" make install

3. 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 port postgresql95-server. I wonder if it would work if I used the existing (and hidden) _postgres user. I also wonder if I should use the --encoding=UTF8 --locale=en_US options I used in my earlier installation.

sudo mkdir -p /opt/local/var/db/postgresql95/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql95/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql95/bin/initdb -D /opt/local/var/db/postgresql95/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.postgresql95-server.plist Disabled -bool false
sudo launchctl load /Library/LaunchDaemons/org.macports.postgresql95-server.plist

Then I did some set-up to get PL/R going.

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

I tend to do my work using a database named “crsp” (originally I just had CRSP data in there) under my own user name. So I set stuff up accordingly. Within psql:

CREATE USER igow SUPERUSER CREATEDB CREATEROLE PASSWORD 'xxxxx';
CREATE EXTENSION plr;
CREATE EXTENSION plperl;
CREATE EXTENSION plpython2u;
CREATE DATABASE crsp OWNER igow;
\q

4. Restore my database from a backup.

/opt/local/lib/postgresql95/bin/psql -U postgres -d postgres -f /Volumes/2TB/data/pg/crsp_all.backup
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s