Using the RPostgreSQL package, is there a way to connect to a remote PostgreSQL instance other than hardcoding the credentials into the dbConnect
method?
As far as I can tell, this is the only way to do it:
dbConnect(drv, host='some.com', port='5432', dbname='some-db', user='usr', password='secrecy')
Certainly there's a way to use a connection string or something?
Edit: I mean referencing some file (separate from the source code) or environment variable that contains the connection string. Sort of like .pgpass
in my home directory, or DATABASE_URL
on heroku. Just some way to avoid having DB credentials in the source code.
On Windows at least, in an interactive session you can prompt the user for a name and password with winDialogString
.
user <- winDialogString("Enter your username", "")
pwd <- winDialogString("Enter your password", "")
dbConnect(..., user=user, password=pwd)
But what does a connection string do that a function call doesn't? Either way, you still have to hardcode your credentials somewhere.
You can also store the credentials in a file somewhere, and read them in using the usual methods (readLines
, scan
, read.table
etc).
### assuming dbcreds.txt contains the single line "username password"
cred <- strsplit(readLines("dbcreds.txt"), "[[:blank:]]+")[[1]]
user <- cred[1]
pwd <- cred[2]
dbConnect(..., user=user, pass=pwd)