2

I'm trying to connect R to a postgresql database that requires SSL.

Here's the connection string that works for me when using PSQL: postgresql://USERNAME:PASSWORD@HOSTNAME:PORT/DBNAME?ssl=true, replacing all the uppercase strings with appropriate values.

In R, I don't know how to handle the SSL parameter. When I try conn = dbConnect(RPostgreSQL::PostgreSQL(), host="HOST", dbname="DBNAME", user="USERNAME", password="PASSWORD", port=PORT)

I get

Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect USERNAME@HOST on dbname "DBNAME" )

When I try conn = dbConnect(RPostgreSQL::PostgreSQL(), host="HOST", dbname="DBNAME", user="USERNAME", password="PASSWORD", port=PORT, ssl=TRUE)

I get

Error in postgresqlNewConnection(drv, ...) : unused argument (ssl = TRUE)

Connect to Postgres via SSL using R suggests adding extra info to the dbname parameter. I've tried dbname="DBNAME ssl=TRUE" which results in RS-DBI driver: (could not connect (null)@HOST on dbname "(null)" I get the same result with sslmode=allow and sslmode=require (as suggested by the above post).

The documentation for the PostgreSQL driver says, under "User Authentication", "The passed string can be empty to use all default parameters, or it can contain one or more parameter settings separated by comma. Each parameter setting is in the form parameter = "value". Spaces around the equal sign are optional." But I haven't been able to get it to accept any parameters other than the three shown in the function prototype.

I'm out of ideas; help appreciated.

Community
  • 1
  • 1
bwbecker
  • 1,031
  • 9
  • 21

1 Answers1

0

You can try this :

  1. Create a configuration file like "configuration.yml" and add your setup : db: host : "your_localhost" dbname : "your_database_name?ssl=true" user : "your_user_name" port : 5432 password : "your_password"

  2. Install this packages : install.packages(yaml, dependencies = TRUE) install.package(RPostgreSQL, dependencies = TRUE) install.packages(DBI, dependencies = TRUE)

  3. Run : driver <- DBI:::dbDriver("PostgreSQL") con <- do.call( RPostgreSQL:::dbConnect, c(drv = driver, yaml:::yaml.load_file("configuration.yml")$db) )

  4. /!\ Note /!\ : don't forget to add this statement *?ssl=true* on the configuration.yml file on the dbname field.

Hope this help !

Sofiane M'barki
  • 193
  • 1
  • 1
  • 11