4

我一直在尝试通过 R 从 PostgreSQL 数据库中查询数据。我尝试用几个不同的包(RODBC, RJDBC, DBI, RPostgres等)给猫剥皮,但我似乎不断收到驱动程序错误。奇怪的是,我从来没有遇到过使用相同的驱动程序/URL 和设置从 SQLWorkbench/J 连接到 Postgres 的问题。

我尝试使用postgresql-9.2-1002.jdbc4.jarandpostgresql-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")

我收到一个错误:

Error in .jfindClass(as.character(driverClass)[1]) : class not found

好的,接下来我尝试使用通用驱动程序:

drv_generic <- dbDriver("PostgreSQL")

奇怪的是,它不希望我输入用户名:

>con <- dbConnect(drv=drv_generic, "jdbc:postgresql://xxx.xxxxx.com", port=xxxx,     uid="xxxx", password="xxxx")
>Error in postgresqlNewConnection(drv, ...) : unused argument (uid = "xxxx")

所以我在没有用户/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")

显然语法错误?

然后我回到尝试“自定义”驱动程序(之前的 .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.

我又试了一次,对语法稍作改动:

con <- dbConnect(drv=drv_custom1, url="jdbc:postgresql://xxxx.xxxx.com", port=xxxx, uid="xxxx", pwd="xxxxx",dsn="xxxx")

并得到同样的错误。我也尝试了许多其他变体/方法。我认为我的部分困惑来自这样一个事实,即在 DBI 之类的包和 RJDBC 等基于它的包之间以非常零碎的方式处理文档,因此当我查看文档时,例如?dbConnect我需要指定的许多选项甚至都没有提到,我一直在根据与这些包/错误相关的杂项 Google 搜索结果进行工作。

我发现的一个线程建议尝试

.jaddClassPath( "xxxxx/postgresql-9.2-1002.jdbc4.jar" )

首先,但这似乎没有帮助。

我也尝试过使用

x <- PostgreSQL(max.con = 16, fetch.default.rec = 500, force.reload = FALSE)

无济于事,我尝试使用 RODBC 作为驱动程序。

更新:

我尝试使用旧版本的驱动程序(jdbc3 而不是 jdbc4),重新启动 R,并分离所有不必要的包。

我能够加载驱动程序

> drv_custom <- JDBC(driverClass = "org.postgresql.Driver", classPath="/xxxxx/xxxxx/postgresql-9.3-1100.jdbc3.jar")

但我仍然无法连接...

> 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
4

2 回答 2

6

这对我有用:

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>")

port诀窍是dbnameurl. 出于某种原因,jdbc:postgresql似乎不喜欢从dbConnect参数中读取这些信息。

  • 如果您不确定它是什么dbname,它可能是postgres.
  • 如果您不确定它是什么port,它可能是5432.

所以一个典型的调用看起来像:

con <- dbConnect(drv, url="jdbc:postgresql://10.10.10.10:5432/postgres", user="<user name>", password="<password>")

您可以从https://jdbc.postgresql.org/jar获取文件

于 2016-10-04T15:58:10.010 回答
4

It took some troubleshooting on IRC, but here's what had to happen:

  1. I needed to clear the workspace, detach RODBC and RJDBC, then restart
  2. Load RPostgreSQL
  3. Use only the generic driver
  4. Modify the syntax to

    con <- dbConnect(drv=drv_generic, "xxx.xxx.com", port=vvvv, user="ffff", password="ffff", dbname="ggg")

Note: removing the jdbc:postgresql: part was key. Those would've been necessary with RJDBC, but RJDBC turned out to be an unnecessarily painful route.

于 2013-11-19T00:23:32.847 回答