How to connect to remote PostgreSQL with R, certificate validation required - r

I'm trying to connect to a remote Postgres database with ssl = verify ca mode. My problem seems to be similar to Connect to Redshift via SSL using R and Connect to Postgres via SSL using R, but they do not work properly. The error is always
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect (null)#datadb1 on dbname "(null)"
My code is something like this
library("RPostgreSQL")
host = 'datadb1'
dbname = 'test'
port = 5432
password = pw
username = 'pep'
pg_dsn = paste0(
'dbname=', dbname, ' ',
'sslrootcert=', "C://root-ca.crt", ' ',
"sslkey=C://pep.key", " ",
"sslcert=C://pep.crt",
'sslmode=verify-ca'
)
dbConnect(RPostgreSQL::PostgreSQL(), dbname=pg_dsn, host=host,
port=port, password=password, user=username)
It's not a general database problem though, because I'm able to connect to the db using Python.
Update: I had made a mistake in specifying the path; the error is actually this:
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect pep#datadb1 on dbname "test")

According to the error message, the problem is that you're passing empty values for the username and database name. That suggests your actual code doesn't match what you've entered here. I would write a 10-line Rscript program that just connects and grabs a bit of data, like this:
#!/usr/bin/Rscript
library("RPostgreSQL")
host = '192.168.36.2'
dbname = 'test'
port = 5432
password = 'secret'
username = 'pep'
pg_dsn = paste(
'dbname=', dbname, ' ',
'sslrootcert=', 'rootCA.pem', ' ',
'sslkey=pem.key', ' ',
'sslcert=pem.crt', ' ',
'sslmode=verify-ca',
sep=""
)
conn <- dbConnect(RPostgreSQL::PostgreSQL(), dbname=pg_dsn, host=host,
port=port, password=password, user=username)
rs <- dbSendQuery(conn, statement="SELECT COUNT(*) FROM users")
data <- fetch(rs, n=1)
dim(data)
So I don't think this is related to SSL certs at all, but the fact that your variables aren't being set the way you think they are.
EDIT: I created my own CA and used it to sign a server cert and a client cert. I put Postgres 9.3 on a fresh VM and have connections working, with certs required on both sides. I can connect with both psql and R. So I'm afraid I can't reproduce your problem. But a few things look suspicious in your code:
You only need one forward slash in your paths, not two. (If you were using backslashes you'd need two.)
You need a space before sslmode, like this:
'sslcert=pem.crt', ' ',
not this:
'sslcert=pem.crt',
Do either of those changes fix your problem?

Related

R Oracle DB connection fails with dbPool but succeeds with dbConnect

I'm attempting to refactor older code to make use of DB pools using the pool package's dbPool function.
Historically I've been using the DBI package's dbConnect function without issue. I can successfully create a connection to my Oracle database with the below code (all credentials are faked):
conn <- DBI::dbConnect(
ROracle::Oracle(),
"database.abcd1234.us-east-1.rds.amazonaws.com/orcl",
username="username",
password="hunter2"
)
However, when I use the same credentials in the same development environment to attempt to create a pool like this:
pool <- pool::dbPool(
drv = ROracle::Oracle(),
dbname = "orcl",
host = "database.abcd1234.us-east-1.rds.amazonaws.com",
username = "username",
password = "hunter2"
)
I get an error:
Error in .oci.Connect(.oci.drv(), username = username, password = password, :
ORA-12162: TNS:net service name is incorrectly specified
I've used dbPool before but with Postgres databases instead of Oracle, and for Postgres, it just worked! I'm thinking that because my credentials work fine for dbConnect, there must be some small thing I'm missing that's needed for dbPool to work correctly too
orcl is the service name, not the database name.
Try:
pool <- pool::dbPool(
drv = ROracle::Oracle(),
host = "database.abcd1234.us-east-1.rds.amazonaws.com/orcl",
username = "username",
password = "hunter2"
)
Or
pool <- pool::dbPool(
drv = ROracle::Oracle(),
sid = "orcl",
host = "database.abcd1234.us-east-1.rds.amazonaws.com",
username = "username",
password = "hunter2"
)

R: NULL value passed as symbol address, error in .Call(RS_PostgreSQL_init, config.params, force) :

I'm new to stackoverflow, so please forgive me if this is not the right place for my question.
When I set up a connectin with PostgreSQL via RPostgreSQL package i get the following error:
error in .Call(RS_PostgreSQL_init, config.params, force) :
NULL value passed as symbol address
after performing the following code
con <- dbConnect(dbDriver("PostgreSQL"), dbname = "dbname", host = "xyz0123456",
port = 5432, user = "Username", password ="password")
The code was working on my old machine. but i fails to work on my new PC.
any idea what this is about?
Thanks in advance!
Franz Hans

R - odbc - how do I close a DB connection?

I'm able to establish the connection fine however I have been unable to figure out how to close the connection once I'm done. Any suggestion?
if (!require('odbc')) install.packages('odbc')
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "localhost",
Database = "VCF",
UID = "**********",
PWD = "**********"
)
dbWriteTable(con, "raw_myserversDF", myserversDF, append = TRUE)
I think this would help you by using RODBC. I also faced the same issue previously
con <- RODBC::odbcDriverConnect(...)
on.exit(RODBC::odbcClose(con))
From r2evans "DBI::dbDisconnect(con). In ?dbConnect, it says: "See Also: 'dbDisconnect()' to disconnect from a database"."

Connect with RJDBC like with odbc

I am trying to connect to my MS SQL database with RJDBC and I don't know what to fill in url argument. With odbc this was enough:
dbConnect(odbc::odbc(),
Driver = "SQL Server",
dsn = "MyDsn",
uid = "User",
pwd = "123456",
server = "myserver123456\\myserver1",
database = "MyDatabase")
When I swap the driver from odbc to jdbc then it fails:
dbConnect(RJDBC::JDBC(classPath = "C:/jdbc/mssql-jdbc-7.0.0.jre8.jar"),
Driver = "SQL Server",
dsn = "MyDsn",
uid = "User",
pwd = "123456",
server = "myserver123456\\myserver1",
database = "MyDatabase")
error: Error in .jcall("java/sql/DriverManager", "Ljava/sql/Connection;", "getConnection", :
argument "url" is missing, with no default
What should i write in url argument? How to get know?
RJDBC uses different arguments for the dbConnect function: a driver definition and a connection URL (the piece you are missing). For example (from https://www.rforge.net/RJDBC/), to connect to a MySQL Database, your code would look like the following:
library(RJDBC)
drv <- JDBC("com.mysql.jdbc.Driver",
"/etc/jdbc/mysql-connector-java-3.1.14-bin.jar",
identifier.quote="`")
conn <- dbConnect(drv, "jdbc:mysql://localhost/test", "user", "pwd")
Loads a JDBC driver for MySQL (adjust the path to the driver's JAR file as necessary) and connects to the local database "test". The connection handle conn is used for all subsequent operations.
For SQL Server, your code will look something like the following (from https://www.r-bloggers.com/connecting-to-sql-server-from-r-using-rjdbc/):
require(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"C:/jdbc/mssql-jdbc-7.0.0.jre8.jar")
conn <- dbConnect(drv, "jdbc:sqlserver://serverName", "userID", "password")

No database selected with RMySQL

I'm trying to connect to remote publicly-accessible MySQL server EnsEMBL public server using RMySQL, but when I try to list the tables, an error occurs:
library(RMySQL)
mydb = dbConnect(MySQL(),
user = 'anonymous',
port = 5306,
host = 'asiadb.ensembl.org')
dbListTables(mydb)
Error in .local(conn, statement, ...) :
could not run statement: No database selected
Is there a a way to find out the name? Or Am I making a completely different mistake altogether?
You have to specify the name of the db in the dbConnect call. e.g. :
mydb = dbConnect(MySQL(),
user = 'anonymous',
port = 5306,
host = 'asiadb.ensembl.org',
db = 'homo_sapiens_core_83_38')
dbListTables(mydb)
It is weird that database = 'testdb' executed with dbExecute in R
db <- dbConnect(RMySQL::MySQL(),
user = 'root',
password = 'pwd123',
host = 'localhost',
database = 'testdb'
)
dbExecute(db, MySQLStatement) # Executed Without Error
But when used dbListTables(db) showing no databases selected.
Changed database into db worked as expected
db <- dbConnect(RMySQL::MySQL(),
user = 'root',
password = 'pwd123',
host = 'localhost',
db = 'testdb'
)

Resources