Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R RPostgreSQL Connect to Remote Postgres Database with SSL

I am trying to connect to a remote PostgreSQL database from within R using the RPostgreSQL package, and I am getting errors that appear to be related to the SSL settings for the connection. I have verified that I can connect from the command line using psql, so I know the connection is valid and accessible from my computer.

My first attempt at connecting in R was the following (where <MyHost> and <MyPass> were filled out appropriately for my connection):

library(RPostgreSQL)
pg <- dbDriver( 'PostgreSQL' )
con = dbConnect( pg, host=<MyHost>, dbname='warehouse', 
                 user='warehouse', password=<MyPass>, port=5432 )

The error I received in R was:

RS-DBI driver: (could not connect warehouse@<MyHost>.com:5432 on 
dbname "warehouse": FATAL:  no pg_hba.conf entry for host "75.166.243.177", 
user "warehouse", database "warehouse", SSL off

I understood this to mean that I needed to apply SSL encryption to my connection, and so I tried the following, which seems to have worked for the OP here: Connect to Postgres via SSL using R

library(RPostgreSQL)
pg <- dbDriver( 'PostgreSQL' )
con = dbConnect( pg, host=<MyHost>, dbname='dbname=warehouse sslmode=require', 
                 user='warehouse', password=<MyPass>, port=5432 )

The error I received then was:

RS-DBI driver: (could not connect warehouse@<MyHost>:5432 on dbname 
"warehouse": sslmode value "require" invalid when SSL support is not 
compiled in

I found this post (Postgres SSLMode Value "require" Invalid When SSL Support Is Not Compiled Using Foreign Data Wrapper) that suggested my build of PostgreSQL may not have had the --with-openssl flag included when it was compiled, but running pg_config revealed that this flag was indeed set in my build. This makes sense because I was able to connect to the database using SSL when I used the psql command line tool.

Fearing my PostgreSQL installation may be at fault, I completely removed it. I then ran both of the above snippets of R code, just for kicks, and I received exactly the same errors. I was not expecting this, but this seems to indicate that either (a) I do not need a local install of PostgreSQL in order to use the RPostgreSQL package or (b) I have a confounding install of PostgreSQL somewhere else on my computer that was not built with the --with-openssl flag included. I have tried locate postgresql at the command line and didn't see anything obvious that looked like a separate install. In any case, I still haven't been able to successfully connect to the remote PostgreSQL database from within R.

I've scoured Stack Overflow for insight here, and it seems there are a number of unresolved questions of a similar nature, where users are unable to establish connections with a remote server for some reason. I tried to install the RPostgres package that Kirill Müller is maintaining here (https://github.com/rstats-db/RPostgres), but I could not get the R package install to recognize my local PostgreSQL install, even after trying to specify the include and lib paths manually using R CMD INSTALL, as the resulting error message suggested (I may have missed something here though...). It sounds like some users have had better luck with RPostgres than with RPostgreSQL, but again, I wasn't able to install this package.

I'm at a loss for what else to do, and am going to have to resort to other technology if I can't figure out how to establish a connection soon. Any help would be greatly appreciated.

like image 714
bmosov01 Avatar asked Sep 04 '17 05:09

bmosov01


People also ask

How do I connect to a PostgreSQL database remotely?

First, you need to configure a PostgreSQL database that you can access remotely over SSL/TLS. (Describing how to configure the PostgreSQL database over SSL/TLS is beyond the scope of this article.) Create a database called keycloak . Note: The name of the database created will appear in the data source's connection URL.

What is the use of rpostgresql?

RPostgreSQL: R Interface to the 'PostgreSQL' Database System Database interface and 'PostgreSQL' driver for 'R'. This package provides a Database Interface 'DBI' compliant driver for 'R' to access 'PostgreSQL' database systems.

How to enable SSL connection in PostgreSQL?

Here are the steps to enable SSL connection in PostgreSQL. On PostgreSQL server, we need 3 certificates in data directory for SSL configuration. They are: Open terminal and run the following command to run as root Generate private key using openssl.

Is there a PostgreSQL driver for R?

Database interface and 'PostgreSQL' driver for 'R'. This package provides a Database Interface 'DBI' compliant driver for 'R' to access 'PostgreSQL' database systems. In order to build and install this package from source, 'PostgreSQL' itself must be present your system to provide 'PostgreSQL' functionality via its libraries and header files.


1 Answers

Thanks for posting! You actually helped us solve it. Here is how we did it:

  • Reboot your Mac into Recovery Mode by restarting your computer and holding down Command+R until the Apple logo appears on your screen.
  • Click Utilities > Terminal.
  • In the Terminal window, type in csrutil disable and press Enter.
  • Restart your Mac.
  • Open a Terminal.
  • Run sudo rm /usr/lib/libpq.5.dylib
  • Run sudo ln -s /Applications/Postgres.app/Contents/Versions/9.6/lib/libpq.5‌​.dylib
  • Now you can connect to the DB.
like image 53
Felipe Cabargas Avatar answered Oct 14 '22 12:10

Felipe Cabargas