Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Install postgres gem on Mac OS X 10.5 (Leopard)

When upgrading or installing recent versions of the postgres ruby gem on Leopard the following error appears

	===========   WARNING   ===========

		You are building this extension on OS X without setting the
		ARCHFLAGS environment variable, and PostgreSQL does not appear
		to have been built as a universal binary. If you are seeing this
		message, that means that the build will probably fail.

		Try setting the environment variable ARCHFLAGS
		to '-arch i386' before building.

		For example:
		(in bash) $ export ARCHFLAGS='-arch i386'
		(in tcsh) $ setenv ARCHFLAGS '-arch i386'

		Then try building again.

		===================================


The trick is to set the ARCHFLAGS variable inside the sudo command using env.

sudo env ARCHFLAGS="-arch i386" gem install postgres

PostgreSQL shared memory

// description of your code here

PostgreSQL 8.1.x/8.2.x works nicely on Leopard but you do have to alter some system settings.

In Mac OS X 10.3 and up you have to increase the shared memory of your system to be able to run PostgreSQL with more connections and be faster in general.
You had to go into /etc/rc and look for similar lines and changing the values accordingly

sysctl -w kern.sysv.shmmax=167772160
sysctl -w kern.sysv.shmmin=1
sysctl -w kern.sysv.shmmni=32
sysctl -w kern.sysv.shmseg=8
sysctl -w kern.sysv.shmall=65536


But in Leopard /etc/rc doesn't exist anymore. You now have to create or open /etc/sysctl.conf
Add the following lines to it:=

kern.sysv.shmmax=167772160
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.sysv.shmall=65536

Install RdbiPgSQL package for R on OS X

Connecting R to Postgres on OS X is a little bit of a hassle given where OS X looks for the libraries to link to.

Assuming you've installed Postgres via MacPorts, add shell variables for your Postgres lib and include directories to ~/.profile

export PG_LIB_DIR=/opt/local/lib/postgresql82
export PG_INCLUDE_DIR=/opt/local/include/postgresql82


Add a link from the OS X SDK directory to /opt

$ sudo ln -s /opt/ /Developer/SDKs/MacOSX10.4u.sdk/opt


Then install RdbiPgSQL as you would normally.

If the install fails because R isn't picking up the PG_LIB_DIR, _INCLUDE_DIR variables, keep a lookout for the lines in the install error output that look like


The downloaded packages are in
	/tmp/RtmpTakYpZ/downloaded_packages


In Terminal, change to this directory, replacing the 'RtmpTakYpZ' string with whatever directory the error output gives. Then run the R install command on the package file:

$ cd /tmp/RtmpTakYpZ/downloaded_packages
$ sudo R CMD INSTALL RdbiPgSQL_1.10.0.tar.gz