While I currently have a workaround method, I feel like there has got to be a better way to do this, something like SSHTunnelForwarder in Python.
(found here) https://sshtunnel.readthedocs.io/en/latest/
My current workaround:
1) Write a /.ssh/config
file that specifies local port forwarding options.
2) In a terminal window, execute ssh -N location_of_db
3) In R, run the following:
library(RPostgres)
drv <- RPostgres::Postgres()
dbName <- "my_database"
host <- "127.0.0.1"
port <- '5439'
user <- "username"
db <- dbConnect(drv, dbname=dbName, host=host, port=port, user=user,
password=readLines("/Users/me/keys/db_password.txt"))
Is there an all R way to do this?
Note: In lieu of 1) and 2) you can just connect and set up local port forwarding with -L
option on the ssh
command (now in the comments), but this requires that you locate your ssh-agent
for the db and provide other security info, as set up by the sys admin.