使用连接字符串的RPostgreSQL dbConnect
问题描述:
使用RPostgreSQL包,除了将凭证硬编码到dbConnect
方法之外,是否有连接到远程PostgreSQL实例的方法?使用连接字符串的RPostgreSQL dbConnect
据我所知,这是做到这一点的唯一方法:
dbConnect(drv, host='some.com', port='5432', dbname='some-db', user='usr', password='secrecy')
当然还有使用一个连接字符串或东西的方法吗?
编辑:我的意思是引用一些文件(与源代码分开)或包含连接字符串的环境变量。有点像在我的主目录中的.pgpass
,或在heroku上的DATABASE_URL
。只是为了避免在源代码中拥有DB凭证。
答
至少在Windows上,在交互式会话中,您可以使用winDialogString
提示用户输入名称和密码。
user <- winDialogString("Enter your username", "")
pwd <- winDialogString("Enter your password", "")
dbConnect(..., user=user, password=pwd)
但是,函数调用不会有什么连接字符串呢?无论哪种方式,您仍然需要在某处硬编码您的凭证。
您也可以存储在一个文件中的凭据的地方,并且在使用通常的方法(readLines
,scan
,read.table
等)阅读。
### 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)
查看RPostgres的文档 - 我想我记录了您需要的文档 – hadley 2015-04-03 02:42:55
谢谢@hadley。我查看了这些文档http://cran.r-project.org/web/packages/RPostgreSQL/RPostgreSQL.pdf,但我没有看到任何连接方式使用的东西,没有证书正确的源代码。你能指点我这样的事情吗? – yalestar 2015-04-03 16:24:19
_RPostgres_,而不是_RPostgreSQL_:https://github.com/rstats-db/RPostgres/blob/master/R/connection.R#L41-L45 – hadley 2015-04-03 19:26:10