Postgres DB无法通过RJDBC连接到R. [英] Postgres DB can't connect to R with RJDBC
问题描述
我一直在尝试通过R查询来自PostgreSQL数据库的数据。我尝试用一些不同的软件包对猫进行换肤( RODBC,RJDBC,DBI,RPostgres
等),但我似乎一直在收到驱动程序错误。奇怪的是,我从来没有使用相同的驱动程序/ URL和设置从SQLWorkbench / J连接到Postgres。
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.
我尝试使用 postgresql -9.2-1002.jdbc4.jar
和 postgresql-9.3-1100.jdbc41.jar
,以及通用 R中的PostgreSQL
驱动程序。两个jar文件分别是(i)我一直使用SQLWorkbench / J的驱动程序和(ii)相同驱动程序的略微更新版本。然而,当我尝试使用它时......
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...
drv_custom <- JDBC(driverClass = "org.postgresql.Driver", classPath="/Users/xxxx/postgresql-9.3-1100.jdbc41.jar")
I得到错误:
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")
所以我尝试没有用户/ uid:
so I try it without user/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")
并收到错误....
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。
Then I circle back to trying the "custom" driver (either one of the .jar files from earlier) but without the driverClass specified.
drv_custom1< - JDBC(classPath =/ Users / xxxx / postgresql-9.2-1002.jdbc4.jar)
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)
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.
我再次尝试了一下,但语法略有改动:
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")
并得到了同样的错误。我尝试了其他一些其他的变化/方法也是如此。我想我的困惑之一来自于这样一个事实,即文档在DBI之类的程序包之间以及像RJDBC之类的程序之间以非常零碎的方式处理,因此当我查看诸如之类的文档?dbConnect
我需要指定的许多选项甚至没有提及,我一直在根据与这些包/错误相关的各种Google搜索结果进行工作。
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.
我找到的一个帖子建议尝试
One thread I found suggested trying
.jaddClassPath( "xxxxx/postgresql-9.2-1002.jdbc4.jar" )
首先,但这似乎没有帮助。
first, but that didn't seem to help.
我也尝试过使用
x <- PostgreSQL(max.con = 16, fetch.default.rec = 500, force.reload = FALSE)
无效,我尝试用RODBC作为驱动程序。
to no avail and I experimented with RODBC as the driver.
更新:
我尝试使用较旧版本的驱动程序(jdbc3而不是jdbc4),重启R并且分离所有你不必要的套餐。
I tried using an older version of the driver (jdbc3 instead of jdbc4), restarting R, and detaching all unnecessary packages.
我能够加载驱动程序
> 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
推荐答案
这对我有用:
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
和 dbname
在 url
中。出于某种原因, jdbc:postgresql
似乎不喜欢从 dbConnect
参数中读取这些信息。
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.
- 如果你不确定
dbname
是什么,它可能是postgres
。 - 如果您不确定
端口
是什么,它可能是5432
。
- If you are not sure what the
dbname
is, it is perhapspostgres
. - If you are not sure what the
port
is, it is perhaps5432
.
所以典型的电话会是这样的:
So a typical call would look like:
con <- dbConnect(drv, url="jdbc:postgresql://10.10.10.10:5432/postgres", user="<user name>", password="<password>")
您可以获得 jar
来自 https://jdbc.postgresql.org/ 的文件
这篇关于Postgres DB无法通过RJDBC连接到R.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!