Postgres DB不能与RJDBC连接到R

时间:2021-11-15 22:47:47

I've been trying to query data from a PostgreSQL DB via R. I've tried skinning the cat with a few different packages (RODBC, RJDBC, DBI, RPostgres, etc), but I seem to keep getting driver errors. Oddly, I never have trouble using the same drivers/URL's and settings to connect to Postgres from SQLWorkbench/J.

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

I've tried using postgresql-9.2-1002.jdbc4.jar and postgresql-9.3-1100.jdbc41.jar, as well as the generic "PostgreSQL" driver in R. The two jar files are the (i) the driver I use all the time with SQLWorkbench/J and (ii) the slightly newer version of that same driver, respectively. Yet, when I try to use it...

我试着使用postgresql - 9.2 - 1002. - jdbc4。jar和postgresql - 9.3 - 1100. - jdbc41。jar和r中的通用的“PostgreSQL”驱动程序一样,这两个jar文件分别是(i)我一直使用的SQLWorkbench/J驱动程序和(ii)相同驱动程序的稍微更新的版本。然而,当我尝试使用它的时候……

drv_custom <- JDBC(driverClass = "org.postgresql.Driver", classPath="/Users/xxxx/postgresql-9.3-1100.jdbc41.jar")

I get an error:

我得到一个错误:

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

OK, so next I try it with the generic driver:

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

drv_generic <- dbDriver("PostgreSQL")

and strangely, it doesn't want me to enter a username:

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

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

so I try it without user/uid:

所以我尝试了没有用户/uid:

con <- dbConnect(drv_generic, "jdbc:postgresql://padb-01.jiwiredev.com:5439", password="paraccel")

con <- dbConnect(drv_generic, "jdbc:postgresql://padb-01.jiwiredev.com:5439", password="paraccel")

and get an error....

和得到一个错误....

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

Apparently the syntax is wrong?

语法显然是错误的?

Then I circle back to trying the "custom" driver (either one of the .jar files from earlier) but without the driverClass specified.

然后,我返回到尝试“自定义”驱动程序(前面的一个.jar文件),但是没有指定驱动程序类。

drv_custom1 <- JDBC( classPath="/Users/xxxx/postgresql-9.2-1002.jdbc4.jar")

drv_custom1 < - JDBC(classPath = " /用户/ xxxx / postgresql - 9.2 - 1002. - jdbc4.jar”)

con <- dbConnect(drv=drv_custom1, "jdbc:postgresql://xxx.xxx.com", port=5439, uid="paraccel", pwd="paraccel")

con <- dbConnect(drv=drv_custom1, "jdbc:postgresql://xxx.xxx.com", port=5439, uid="paraccel", pwd="paraccel")

and get this error:

并得到这个错误:

Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1],  : 
RcallMethod: attempt to call a method of a NULL object.

I tried it again with a slight alteration to the syntax:

我又试了一次,稍微改动了一下语法:

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

and got the same error. I tried a number of other variations/approaches as well. I think part of my confusion comes from the fact that the documentation is handled in a very piecemeal way between packages like DBI and those that build upon it like RJDBC, so that when I look at documentation such as ?dbConnect many of the options I need to specify are not even mentioned, and I've been working based off of miscellaneous Google search results related to these packages/errors.

得到了同样的错误。我也尝试了一些其他的变化/方法。我认为我混乱的一部分来自这样一个事实:文档包之间以一种非常零碎的方式处理DBI和那些像RJDBC建立它,所以当我看文档如? dbConnect的许多选项我需要指定甚至没有提到的,和我所工作的杂项Google搜索结果相关的这些包/错误。

One thread I found suggested trying

我找到一个线索,建议试试

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

first, but that didn't seem to help.

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

I also tried using

我也试着用

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

to no avail and I experimented with RODBC as the driver.

但没有任何效果,我尝试用RODBC做司机。

UPDATE:

更新:

I tried using an older version of the driver (jdbc3 instead of jdbc4), restarting R, and detaching all unnecessary packages.

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

I was able to load the driver

我给司机上了车

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

but I still can't connect...

但是我还是联系不上……

> 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

2 个解决方案

#1


4  

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

它在IRC上进行了一些故障排除,但是以下是必须发生的事情:

  1. I needed to clear the workspace, detach RODBC and RJDBC, then restart
  2. 我需要清除工作空间,分离RODBC和RJDBC,然后重新启动。
  3. Load RPostgreSQL
  4. 负载RPostgreSQL
  5. Use only the generic driver
  6. 只使用通用驱动程序
  7. Modify the syntax to

    修改语法

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

    con <- dbConnect(drv=drv_generic, "xxx.xxx.com", port=vvv, 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.

注意:删除jdbc:postgresql:部分是关键。这些对于RJDBC来说是必要的,但是RJDBC被证明是一个不必要的痛苦路径。

#2


4  

This works for me:

这工作对我来说:

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

The trick was to include port and dbname in the url. For some reason, jdbc:postgresql does not seem to like reading those information from the dbConnect parameters.

诀窍是在url中包含端口和dbname。出于某种原因,jdbc:postgresql似乎不喜欢从dbConnect参数读取这些信息。

  • If you are not sure what the dbname is, it is perhaps postgres.
  • 如果您不确定dbname是什么,那么可能是postgres。
  • If you are not sure what the port is, it is perhaps 5432.
  • 如果你不确定端口是什么,可能是5432。

So a typical call would look like:

所以一个典型的电话是这样的:

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

You can get the jar file from https://jdbc.postgresql.org/

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

#1


4  

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

它在IRC上进行了一些故障排除,但是以下是必须发生的事情:

  1. I needed to clear the workspace, detach RODBC and RJDBC, then restart
  2. 我需要清除工作空间,分离RODBC和RJDBC,然后重新启动。
  3. Load RPostgreSQL
  4. 负载RPostgreSQL
  5. Use only the generic driver
  6. 只使用通用驱动程序
  7. Modify the syntax to

    修改语法

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

    con <- dbConnect(drv=drv_generic, "xxx.xxx.com", port=vvv, 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.

注意:删除jdbc:postgresql:部分是关键。这些对于RJDBC来说是必要的,但是RJDBC被证明是一个不必要的痛苦路径。

#2


4  

This works for me:

这工作对我来说:

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

The trick was to include port and dbname in the url. For some reason, jdbc:postgresql does not seem to like reading those information from the dbConnect parameters.

诀窍是在url中包含端口和dbname。出于某种原因,jdbc:postgresql似乎不喜欢从dbConnect参数读取这些信息。

  • If you are not sure what the dbname is, it is perhaps postgres.
  • 如果您不确定dbname是什么,那么可能是postgres。
  • If you are not sure what the port is, it is perhaps 5432.
  • 如果你不确定端口是什么,可能是5432。

So a typical call would look like:

所以一个典型的电话是这样的:

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

You can get the jar file from https://jdbc.postgresql.org/

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