Jas*_*son 4 java postgresql r jdbc rjdbc
我一直试图通过R从PostgreSQL数据库查询数据.我尝试用一些不同的软件包(RODBC, RJDBC, DBI, RPostgres等等)对猫进行换肤,但我似乎不断收到驱动程序错误.奇怪的是,我从来没有遇到过使用相同的驱动程序/ URL和设置从SQLWorkbench/J连接到Postgres的麻烦.
我已尝试使用postgresql-9.2-1002.jdbc4.jar和postgresql-9.3-1100.jdbc41.jar,以及"PostgreSQL"R中的通用驱动程序.两个jar文件分别是(i)我一直使用SQLWorkbench/J的驱动程序和(ii)相同驱动程序的稍新版本.然而,当我尝试使用它时......
drv_custom <- JDBC(driverClass = "org.postgresql.Driver", classPath="/Users/xxxx/postgresql-9.3-1100.jdbc41.jar")
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Error in .jfindClass(as.character(driverClass)[1]) : class not found
Run Code Online (Sandbox Code Playgroud)
好的,接下来我尝试使用通用驱动程序:
drv_generic <- dbDriver("PostgreSQL")
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它不希望我输入用户名:
>con <- dbConnect(drv=drv_generic, "jdbc:postgresql://xxx.xxxxx.com", port=xxxx, uid="xxxx", password="xxxx")
>Error in postgresqlNewConnection(drv, ...) : unused argument (uid = "xxxx")
Run Code Online (Sandbox Code Playgroud)
所以我没有用户/ uid尝试:
con < - dbConnect(drv_generic,"jdbc:postgresql://padb-01.jiwiredev.com:5439",password ="paraccel")
并得到一个错误....
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect jdbc:postgresql://padb-01.xxx.com:5439@local on dbname "jdbc:postgresql://xxxx.xxxx.com:5439")
Run Code Online (Sandbox Code Playgroud)
显然语法错了?
然后我回过头来尝试"自定义"驱动程序(前面的.jar文件之一),但没有指定driverClass.
drv_custom1 < - JDBC(classPath ="/ Users/xxxx/postgresql-9.2-1002.jdbc4.jar")
con < - dbConnect(drv = drv_custom1,"jdbc:postgresql://xxx.xxx.com",port = 5439,uid ="paraccel",pwd ="paraccel")
并得到此错误:
Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
RcallMethod: attempt to call a method of a NULL object.
Run Code Online (Sandbox Code Playgroud)
我再次尝试了一下,对语法略有改动:
con <- dbConnect(drv=drv_custom1, url="jdbc:postgresql://xxxx.xxxx.com", port=xxxx, uid="xxxx", pwd="xxxxx",dsn="xxxx")
Run Code Online (Sandbox Code Playgroud)
并得到了同样的错误.我也尝试了许多其他的变化/方法.我认为我的困惑部分来自于这样一个事实:文档在DBI之类的包之间以及像RJDBC之类的包之间以非常零碎的方式处理,因此当我查看文档时,例如?dbConnect我需要指定的许多选项甚至没有提到,我一直在根据与这些包/错误相关的各种Google搜索结果开展工作.
我找到的一个帖子建议尝试
.jaddClassPath( "xxxxx/postgresql-9.2-1002.jdbc4.jar" )
Run Code Online (Sandbox Code Playgroud)
首先,但这似乎没有帮助.
我也试过用
x <- PostgreSQL(max.con = 16, fetch.default.rec = 500, force.reload = FALSE)
Run Code Online (Sandbox Code Playgroud)
没有用,我试验了RODBC作为驱动程序.
更新:
我尝试使用较旧版本的驱动程序(jdbc3而不是jdbc4),重新启动R,并分离所有不必要的包.
我能够加载驱动程序
> drv_custom <- JDBC(driverClass = "org.postgresql.Driver", classPath="/xxxxx/xxxxx/postgresql-9.3-1100.jdbc3.jar")
Run Code Online (Sandbox Code Playgroud)
但我仍然无法连接......
> con <- dbConnect(drv=drv_custom, "jdbc:postgresql://xxxxx.xxxxx.com", port=5439, uid="xxxxx", pwd="xxxxx")
Error in .verify.JDBC.result(jc, "Unable to connect JDBC to ", url) :
Unable to connect JDBC to jdbc:postgresql://xxxxx.xxxx.com
Run Code Online (Sandbox Code Playgroud)
这对我有用:
library(RJDBC)
drv <- JDBC("org.postgresql.Driver","C:/R/postgresql-9.4.1211.jar")
con <- dbConnect(drv, url="jdbc:postgresql://host:port/dbname", user="<user name>", password="<password>")
Run Code Online (Sandbox Code Playgroud)
诀窍是包括port和dbname在url.出于某种原因,jdbc:postgresql似乎不喜欢从dbConnect参数中读取这些信息.
dbname它是什么,也许是postgres.port它是什么,也许是5432.所以典型的调用看起来像:
con <- dbConnect(drv, url="jdbc:postgresql://10.10.10.10:5432/postgres", user="<user name>", password="<password>")
Run Code Online (Sandbox Code Playgroud)
您可以jar从https://jdbc.postgresql.org/获取该文件
| 归档时间: |
|
| 查看次数: |
7142 次 |
| 最近记录: |