Postgres DB can't connect to R with RJDBC

Jason picture Jason · Nov 18, 2013 · Viewed 7.8k times · Source

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.

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

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.

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

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.

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.

UPDATE:

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

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

Answer

user2782503 picture user2782503 · Oct 4, 2016

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.

  • If you are not sure what the dbname is, it is perhaps postgres.
  • If you are not sure what the port is, it is perhaps 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/