Install R Package RPostgreSQL in Mac OSX
I am trying to use PostgreSQL for data warehousing data and R for interactive data exploration. So the first priority to make this happen is to connect PostgreSQL from R and read data back via customized SQL.
As discussed at stackoverflow, the RPostgreSQL seems to be the best choice for its maturity and standard compliance. But there is no official binary for Mac OSX, so I have to do it myself to make it work.
First of all, my PostgreSQL is installed via MacPorts. I am using the 8.4 version. And I am using the binary version of R downloaded from its homepage. I prefer the binary version over the MacPorts version because the binary version has a native GUI, and doesn’t depend on X11. The problem of installing RPostgreSQL comes along with the different sources between PostgreSQL and R.
I downloaded the RPostgreSQL source code from the project page and issue:
install.packages(c("/Users/<you>/Downloads/RPostgreSQL_0.1-6.tar.gz"), type="source"))
The configure script fails to find the libpq-fe.h file. That is because my PostgreSQL is installed at /opt/local/ prefix, which is not set to the default location for searching include and lib files at RPostgreSQL’s configure script.
Solution for the installation is simple. After reading the RPostgreSQL’s configure script. I found that I can use pg_config to let the configure script discover the installation of RPostgreSQL automatically. But there is still a problem that the pg_config doesn’t resides at ‘/opt/local/lib/postgresql84/bin’ which is not in the standard executable search paths. So, I made a temporary link of the pg_config, like
sudo ln -s /opt/local/lib/postgresql84/bin/pg_config /usr/local/bin/
Then re-issuing the above install command succeeded.
In sum,
- You need to install RPostgreSQL package from source due to lack of precompiled binary in Mac OSX
- If your R and PostgreSQL are installed from difference Sources (like me, one from binary, the other via MacPorts) or R prompts that libpq-fe.h cannot be found, check your pg_config whether is in the executable search paths.
- If your pg_config cannot be searched, make it happen by simply making a soft link (my method) or put the path of pg_config into the PATH environment.
- Issue the ordinary install command in R environment.
Finally, to test whether the installation succeeded, type
library(RPostgreSQL)
Hi, I have postgres and r installed just like you, however I use postgres 9.0
I tried to follow your guide but still get the
checking for “/libpq-fe.h”… no
configure: error: File libpq-fe.h not in ; installation may be broken.
I tried to do
sudo ln -s /Library/postgresql/9.0/bin/pg_config /usr/local/bin/
but this doesn’t change anything, same error. Do you have any idea what this could be?
Thanks for sharing your knowledge!
Jon
Hi Jon,
You seem to be using the DMG precompiled PostgreSQL installation, aren’t you?
Maybe your can check the output of ‘pg_config’, and ensure whether ‘libpg-fe.h’ is installed.
Again I tried postgresql90 from MacPorts, my method works.
Thanks,
Actually, I did use macports for postgresql installation.
the libpq-fe.h and pg_config.hfile is in folder /Library/postgresql/9.0/include/ but issuing:
sudo ln -s /Library/postgresql/9.0/include/pg_config /usr/local/bin/
gives me output: ln: /usr/local/bin//pg_config: File exists
and R still can’t find libpg-fe.h
checking for “/libpq-fe.h”… no
configure: error: File libpq-fe.h not in ; installation may be broken.
Its probably something simple I am doing wrong, I am novice at command line installations and such :-/
sorry, another novice question, how do you mean “Maybe your can check the output of ‘pg_config’, and ensure whether ‘libpg-fe.h’ is installed”?
tried port install libpqxx….
then install package in R gives
install.packages(c(“/Users/jon/Downloads/RPostgreSQL_0.1-6.tar.gz”), type=”source”)
inferring ‘repos = NULL’ from the file name
During startup – Warning messages:
1: Setting LC_CTYPE failed, using “C”
2: Setting LC_TIME failed, using “C”
3: Setting LC_MESSAGES failed, using “C”
* installing *source* package ‘RPostgreSQL’ …
checking for gcc… gcc
checking for C compiler default output file name… a.out
checking whether the C compiler works… yes
checking whether we are cross compiling… no
checking for suffix of executables…
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes
checking for gcc option to accept ISO C89… none needed
checking for pg_config… /usr/local/bin/pg_config
checking for “/opt/local/include/postgresql84/libpq-fe.h”… yes
configure: creating ./config.status
config.status: creating src/Makevars
** libs
** arch – x86_64
gcc -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 -I/opt/local/include/postgresql84 -I/usr/local/include -fPIC -g -O2 -c RS-DBI.c -o RS-DBI.o
gcc -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 -I/opt/local/include/postgresql84 -I/usr/local/include -fPIC -g -O2 -c RS-PostgreSQL.c -o RS-PostgreSQL.o
gcc -arch x86_64 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o RPostgreSQL.so RS-DBI.o RS-PostgreSQL.o -L/opt/local/lib/postgresql84 -lpq -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld warning: in /opt/local/lib/postgresql84/libpq.dylib, file is not of required architecture
** R
** inst
** preparing package for lazy loading
Warning: package ‘DBI’ was built under R version 2.10.1
Creating a new generic function for “format” in “RPostgreSQL”
Creating a new generic function for “print” in “RPostgreSQL”
** help
*** installing help indices
** building package indices …
* DONE (RPostgreSQL)
loading library gives:
library(RPostgreSQL)
Error in dyn.load(file, DLLpath = DLLpath, …) :
unable to load shared library ‘/Library/Frameworks/R.framework/Resources/library/RPostgreSQL/libs/x86_64/RPostgreSQL.so’:
dlopen(/Library/Frameworks/R.framework/Resources/library/RPostgreSQL/libs/x86_64/RPostgreSQL.so, 6): Symbol not found: _PQbackendPID
Referenced from: /Library/Frameworks/R.framework/Resources/library/RPostgreSQL/libs/x86_64/RPostgreSQL.so
Expected in: dynamic lookup
Error: package/namespace load failed for ‘RPostgreSQL’
stuck now. Sorry for posting so much here, just delete if its annoying. really would like postgres to work well with R but its fairly high tech to get it to work it seems
Jon
Hi Jon,
It seems that we are using different versions of R and OSX. I run R 2.11.1 at OSX 10.5 Leopard 32bit version.
And my ‘RPostgreSQL.so’ locates at ‘/Users//Library/R/2.11/library/RPostgreSQL/libs/i386′.
The otool output of RPostgreSQL.so looks like:
Hope the information helps.
Another option for integrating R and PostgreSQL is RpgSQL. You can find my another post about it at http://blog.chrischou.org/2010/09/28/manipulate-data-in-r-via-sql-interface/
Hello, I have postgres and r installed. more specifically, i have postgres 9, from the precompiled DMG installation. Excuse me for what i might be an ignorant question, but i can’t find pg_config on my system. Is this because i used the precompiled DMG?
Thanks! I finally installed it.
thanks for this entry, solved my install problem!
RPostreSQL seemed to break with R 2.13. But I got it to work by downloading the package from CRAN (http://cran.r-project.org/web/packages/RPostgreSQL/index.html) to my ~/Downloads folder. Then (from ~/Downloads) I enter the following into the terminal:
tar xvzf RPostgreSQL_0.1-7.tar
followed by editing the configure file in the ~/Downloads/RPostgreSQL directory as follows. Find the portion of the file that looks like this:
if ! test $PG_INCDIR
then
for dir in \
/usr/include \
then add the path to libpq-fe.h to the list of directories searched. After editing, it looks like this:
if ! test $PG_INCDIR
then
for dir in \
/Library/PostgreSQL/9.0/include \
/usr/include \
Then I went to R and entered:
install.packages(“~/Downloads/RPostgreSQL”, repos = NULL, type=”source”)
Voila!
Actually I omitted a step in my earlier instructions. Before editing the ~/Downloads/configure file, I did the following (a modified version of what is suggested above to reflect the different path for the later PostgreSQL):
sudo ln -s /Library/PostgreSQL/9.0/bin/pg_config /usr/local/bin
Many thanks- easy fix!
RPostgreSQL doesn’t know where to look for postgres 9.0
to fix:
tar zxf RPostgreSQL_0.1-7.tar.gz
cd RPostgreSQL
sed -i -e ‘s/8.4SS/9.0/g’ configure.in
autoconf
cd ../
R CMD build RPostgreSQL
R CMD INSTALL RPostgreSQL_0.1-7.tar.gz
This worked for me as well. Thanks for sharing!