其实大家在Linux 的R 中安装其他库,完全可以使用R 自带的安装方式,只是这个 RPostgreSQL 库需要用到 postgresql 的lib 库与include 头文件,所以才有若干个步骤去准备。

我自己的做法是这样的,首先在网上下载一个 RPostgreSQL 的源码包,源码下载地址:https://cran.r-project.org/src/contrib/RPostgreSQL_0.4-1.tar.gz ,解压源码包后,里面会有一个 configure 的文件,我们可以通过这个configure 文件来检测我们的环境是否已经可以安装 RPostgreSQL 库了。

例如我自己在检测时,发现我的环境了没有 postgresql 的lib 和include 相关文件,开始时的错误信息

checking for "/libpq-fe.h"... no

其实我自己的环境中,是有postgresql 的相关文件的,只是我不是通过 apt-get 或者是源码安装的方式来部署我自己的 postgresql 环境而已。

我的postgresql HOME 目录在 /home/sdbadmin/ssql,而且也能在ssql 的目录中看到 lib 和 include 目录,所以我自己就直接将文件拷贝到 /usr 路径下了(需要root 权限)

cp -rf /home/sdbadmin/ssql/include/* /usr/include/
cp -rf /home/sdbadmin/ssql/lib/* /usr/lib/

再来执行configure 文件,就能够通过了

root@chen:~/tmp/RPostgreSQL# ./configure
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 build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for pg_config... no
configure: checking for PostgreSQL header files
configure: Checking include /usr/include.
configure: Checking lib /usr/lib.
checking for "/usr/include/libpq-fe.h"... yes
configure: creating ./config.status
config.status: creating src/Makevars

然后我们就能放心在R 中安装 RPostgreSQL 库了

直接通过 Shell 进入R 环境,进入方式 就是大写 的R

执行安装命令

install.packages('RPostgreSQL')

它会让你选在一个下载方式,我自己随便选了个 2

--- Please select a CRAN mirror for use in this session ---
HTTPS CRAN mirror : -Cloud [https] : Algeria [https]
: Austria [https] : Belgium (Ghent) [https]
: Brazil (SP ) [https] : Canada (MB) [https]
: Chile [https] : China (Beijing ) [https]
: Colombia (Cali) [https] : France (Lyon ) [https]
: France (Lyon ) [https] : France (Paris ) [https]
: Germany (Münster) [https] : Iceland [https]
: Italy (Padua) [https] : Japan (Tokyo) [https]
: Malaysia [https] : Mexico (Mexico City) [https]
: New Zealand [https] : Russia (Moscow) [https]
: Serbia [https] : Spain (A Coruña) [https]
: Spain (Madrid) [https] : Switzerland [https]
: UK (Bristol) [https] : UK (Cambridge) [https]
: USA (CA ) [https] : USA (KS) [https]
: USA (MI ) [https] : USA (TN) [https]
: USA (TX) [https] : USA (WA) [https]
: (HTTP mirrors)

Selection: 2

它就开始扒拉扒拉 开始下载了

also installing the dependency ‘DBI’

trying URL 'https://cran.usthb.dz/src/contrib/DBI_0.4-1.tar.gz'
Content type 'application/x-gzip' length bytes ( KB)
==================================================
downloaded KB trying URL 'https://cran.usthb.dz/src/contrib/RPostgreSQL_0.4-1.tar.gz'
Content type 'application/x-gzip' length bytes ( KB)
==================================================
downloaded KB * installing *source* package ‘DBI’ ...
** package ‘DBI’ successfully unpacked and MD5 sums checked
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (DBI)
* installing *source* package ‘RPostgreSQL’ ...
** package ‘RPostgreSQL’ successfully unpacked and MD5 sums checked
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 build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for pg_config... no
configure: checking for PostgreSQL header files
configure: Checking include /usr/include.
configure: Checking lib /usr/lib.
checking for "/usr/include/libpq-fe.h"... yes
configure: creating ./config.status
config.status: creating src/Makevars
** libs
gcc -std=gnu99 -I/usr/local/lib/R/include -DNDEBUG -I/usr/include -I/usr/local/include -fpic -g -O2 -c RS-DBI.c -o RS-DBI.o
gcc -std=gnu99 -I/usr/local/lib/R/include -DNDEBUG -I/usr/include -I/usr/local/include -fpic -g -O2 -c RS-PQescape.c -o RS-PQescape.o
gcc -std=gnu99 -I/usr/local/lib/R/include -DNDEBUG -I/usr/include -I/usr/local/include -fpic -g -O2 -c RS-PostgreSQL.c -o RS-PostgreSQL.o
RS-PostgreSQL.c: In function ‘RS_PostgreSQL_createDataMappings’:
RS-PostgreSQL.c::: warning: passing argument of ‘Rf_protect’ from incompatible pointer type [enabled by default]
/usr/local/lib/R/include/Rinternals.h::: note: expected ‘SEXP’ but argument is of type ‘struct RS_DBI_fields *’
gcc -std=gnu99 -I/usr/local/lib/R/include -DNDEBUG -I/usr/include -I/usr/local/include -fpic -g -O2 -c RS-pgsql-copy.c -o RS-pgsql-copy.o
gcc -std=gnu99 -I/usr/local/lib/R/include -DNDEBUG -I/usr/include -I/usr/local/include -fpic -g -O2 -c RS-pgsql-getResult.c -o RS-pgsql-getResult.o
gcc -std=gnu99 -I/usr/local/lib/R/include -DNDEBUG -I/usr/include -I/usr/local/include -fpic -g -O2 -c RS-pgsql-pqexec.c -o RS-pgsql-pqexec.o
gcc -std=gnu99 -I/usr/local/lib/R/include -DNDEBUG -I/usr/include -I/usr/local/include -fpic -g -O2 -c RS-pgsql-pqexecparams.c -o RS-pgsql-pqexecparams.o
gcc -std=gnu99 -shared -L/usr/local/lib -o RPostgreSQL.so RS-DBI.o RS-PQescape.o RS-PostgreSQL.o RS-pgsql-copy.o RS-pgsql-getResult.o RS-pgsql-pqexec.o RS-pgsql-pqexecparams.o -L/usr/lib -lpq
installing to /usr/local/lib/R/library/RPostgreSQL/libs
** R
** inst
** preparing package for lazy loading
Creating a generic function for ‘format’ from package ‘base’ in package ‘RPostgreSQL’
Creating a generic function for ‘print’ from package ‘base’ in package ‘RPostgreSQL’
Creating a generic function for ‘summary’ from package ‘base’ in package ‘RPostgreSQL’
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (RPostgreSQL) The downloaded source packages are in
‘/tmp/Rtmp4P3XQb/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done

RPostgreSQL 安装好了之后,当然是先试试能不能工作了

执行如下命令

require(RPostgreSQL)
drv = dbDriver("PostgreSQL")
pgdb = dbConnect(drv, user="pgadmin", password="pgadmin",host="192.168.57.132", dbname="foo")

执行到 dbConnect 时,报错

Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect pgadmin@192.168.57.132 on dbname "foo"

细想,应该是postgresql 的访问权限问题,导致外来的客户端无法直接连接数据库

手工对postgresql 停服务,修改 postgresql 的配置参数,我自己的postgresql 环境,所有配置文件都在 pg_data 目录下,执行以下命令时,是在 /home/sdbadmin/ssql 目录下

  sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '0.0.0.0'/g" pg_data/postgresql.conf ;
linenum=$(cat -n pg_data/pg_hba.conf | grep "# IPv4 local connections:" | awk '{print $1}');
let "linenum=linenum+1";varStr="host all all 0.0.0.0/0 trust";
sed -i "${linenum} a${varStr}" pg_data/pg_hba.conf;

启动postgresql 服务

bin/postgres -D pg_data/ >> logfile >& &

再回过头来测试 RPostgreSQL 库

pgdb = dbConnect(drv, user="pgadmin", password="pgadmin",host="192.168.57.132", dbname="foo")

通过了,然后在里面查找数据

dbGetQuery(pgdb, "select * from t2")

成功返回记录

  name
ttt

到这里就完全介绍完 RPostgreSQL 库如何安装和与 postgresql 进行对接,希望能够对大家有所帮助

参考博客:

http://blog.chinaunix.net/uid-14874549-id-3457872.html

http://www.01yun.com/dotnet/20130711/571315.html

http://stackoverflow.com/questions/22202141/installing-rpostgresql-on-linux

http://www.sequoiadb.com/cn/index.php?a=index&m=Files&cat_id=1432190716&edition_id=0

ubuntu 安装 RPostgreSQL 库的更多相关文章

  1. 【并行】Ubuntu安装MPI库

    1.环境搭建 终端:sudo apt-get install mpich2 安装的是mpich2的1.4.1版本. 2.编译 终端:mpicc mpi_hello.c -g -Wall -o mpi_ ...

  2. Ubuntu 安装boost 库

    使用 apt-get进行安装 sudo apt-get install libboost-dev

  3. Ubuntu安装usb库

    sudo pip install pyserial sudo pip install pyusb

  4. ubuntu 下安装boost库

    ubuntu下安装boost库,,在网上试了一些其他人推荐的libboost-dev 但是会缺少,编译程序会报错: /usr/bin/ld: cannot find -lboost_serializa ...

  5. Ubuntu 16.04 安装PCL库以及测试

    参考链接:https://blog.csdn.net/dantengc/article/details/78446600 参考博客,官网一直安装不成功,后来参照一篇博客终于安装成功了,记录如下. 1. ...

  6. Ubuntu安装MySQL和Python库MySQLdb步骤

    一.安装MySQL服务器和客户端 执行以下命令: sudo apt-get install mysql-server-5.6 mysql-client-5.6 sudo apt-get install ...

  7. 在Ubuntu上安装boost库[转]

    在编译kenlm的时候需要安装boost,去官网下载boost安装包,然后按照以下步骤安装. boost官网 -----------------以下内容,网上转载------------------- ...

  8. Ubuntu下dlib库编译安装

    安装libboost 按照dlib的说明安装始终不成功,参考machine learning is fun作者的指导installing_dlib_on_macos_for_python.md,需要首 ...

  9. Ubuntu 安装MySQL报共享库找不到

    错误信息1: ./mysqld: error : cannot open shared object file: No such file or directory 解决办法:安装改库 # apt-g ...

随机推荐

  1. Liquibase

    http://www.liquibase.org/documentation/index.html https://github.com/studygolang/studygolang/tree/ma ...

  2. [Java SE] 字符串连接

    Java 支持多种字符串连接方式,总结如下: package cn.spads.tool.string; import java.text.MessageFormat; /** * <b> ...

  3. 有关 java 不定参数

    不定参数实际为数组参数的一种写法而已,本质上与数组参数完全相同 //1.数组参数函数 public static int sum(int[] values) { } //2.不定参数函数 不定参数只能 ...

  4. RobotFramework教程使用笔记——RIDE的相关知识及Resources创建关键字文件

    RIDE是robotframework的图形操作前端,我们在RIDE上进行测试用例设计和编写测试脚本,并执行自动化测试.下面来全面的认识下这个操作工具. 在右边编辑页面有三大模块,Edit,TextE ...

  5. tomcat启动项目被重新加载,导致资源初始化两遍

    之前没有遇到过这个问题,配了三天的项目了,惊人啊!!!各种怪问题全被我赶上了.真有种骂人的冲动. tomcat启动项目时,项目资源被加载两遍. 原因:配置虚拟目录导致,项目被重新加载. <Hos ...

  6. MySql服务未知原因消失了的终极解决办法

    刚才突然MySQL-Front打开不了MySql,果不其然发现是MySql的服务不见了.想起可能是昨晚安装了PhpStudy的点了不要MySql的选项,它把我本身的MYSQL服务给卸载了. 解决办法  ...

  7. HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)

    You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...

  8. Ubuntu+win7 双系统修改开机启动项顺序

    Ubuntu和windows双系统安装完后默认Ubuntu系统是第一启动项,等待时间是10秒 如果你想改成windows为第一启动项 先进去Ubuntu系统 打开终端 (Ctrl+Alt+T) 修改启 ...

  9. 蓝桥杯 2014本科C++ B组 奇怪的分式 暴力枚举

    蓝桥杯 枚举 奇怪的分式 标题:奇怪的分式 上小学的时候,小明经常自己发明新算法.一次,老师出的题目是: 1/4 乘以 8/5 小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1 ...

  10. mina框架之---服务端NioSocketAcceptor的学习

    接上一讲对mina的简单应用和对mina服务端和客户端中几个重要的技术知识点的理解后,今天着重对mina服务端的NioSocketAcceptor 进行学习. 说这个玩意之前,先整体上看一下在mina ...