R Shiny 使用 .pgpass 破坏 PostgreSQL 身份验证
R Shiny breaks PostgreSQL authentication with .pgpass
我的数据库密码存储在 pgpass.conf
文件中。我使用 RPostgres
从 R 连接到数据库,没有指定密码,所以它是从 pgpass.conf
读取的,如下所示:
con <- dbConnect(RPostgres::Postgres(),
dbname = "dbname",
user = "username",
host = "localhost",
port = "5432")
它通常运行良好,但是当我尝试从闪亮的应用程序连接到数据库时它不起作用。连接定义和上面完全一样,放在server.R
脚本中。当我 运行 使用默认参数的 Shiny 应用程序时,出现错误:
FATAL: password authentication failed for user "username"
password retrieved from file "C:\Users\...\AppData\Roaming/postgresql/pgpass.conf"
当连接定义中明确给出密码时:
con <- dbConnect(RPostgres::Postgres(),
dbname = "dbname",
user = "username",
host = "localhost",
password = "mypass",
port = "5432")
一切正常。
更奇怪的是,当 shiny 的端口设置为某个值时,例如:shiny::runApp(port = 4000)
,连接建立时没有指定密码,但仅限于第一次 - 这意味着当应用程序关闭并且在同一 R 会话中重新打开,错误再次发生。
我已经测试了软件包 'RPostgreSQL' - 它也不起作用,只有错误消息不同:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect postgres@localhost on dbname "dbname")
我使用 32 位 R,但我已经在 64 位上测试过它,结果是一样的。闪亮的应用程序在浏览器 (Chrome) 和 Rstudio Viewer 中都是 运行。
这是我的会话信息:
R version 3.2.2 (2015-08-14)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Polish_Poland.1250 LC_CTYPE=Polish_Poland.1250 LC_MONETARY=Polish_Poland.1250
[4] LC_NUMERIC=C LC_TIME=Polish_Poland.1250
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RPostgres_0.1 DBI_0.3.1.9008 shiny_0.12.2
loaded via a namespace (and not attached):
[1] R6_2.1.1 htmltools_0.2.6 tools_3.2.2 rstudioapi_0.3.1 Rcpp_0.12.1.3 jsonlite_0.9.17 digest_0.6.8
[8] xtable_1.7-4 httpuv_1.3.3 mime_0.4 RPostgreSQL_0.4
Shiny 和您的系统 R GUI 之间的命令 运行 环境可能有所不同。我通过将我的凭据存储在 Renviron file:
中来解决这个问题
readRenviron("~/.Renviron")
con <- dbConnect(RPostgres::Postgres(),
dbname = Sys.getenv('pg_db'),
user = Sys.getenv('api_user'),
...)
关键是您可以为暂存和生产环境维护单独的 Renviron。这允许您的脚本使用 commandArgs()
来指定它应该使用的数据库凭据:
#!/usr/bin/env Rscript
environ_path <- switch(commandArgs(),
'staging' = {"~/staging.Renviron"},
'production' = {"~/production/Renviron"})
readRenviron(environ_path)
然后从BASH:
Rscript analysis.R staging
错误在 Postgresql 中:
C:\Users\...\AppData\Roaming/postgresql/pgpass.conf
文件路径包含“/”而不是“\”
我的数据库密码存储在 pgpass.conf
文件中。我使用 RPostgres
从 R 连接到数据库,没有指定密码,所以它是从 pgpass.conf
读取的,如下所示:
con <- dbConnect(RPostgres::Postgres(),
dbname = "dbname",
user = "username",
host = "localhost",
port = "5432")
它通常运行良好,但是当我尝试从闪亮的应用程序连接到数据库时它不起作用。连接定义和上面完全一样,放在server.R
脚本中。当我 运行 使用默认参数的 Shiny 应用程序时,出现错误:
FATAL: password authentication failed for user "username"
password retrieved from file "C:\Users\...\AppData\Roaming/postgresql/pgpass.conf"
当连接定义中明确给出密码时:
con <- dbConnect(RPostgres::Postgres(),
dbname = "dbname",
user = "username",
host = "localhost",
password = "mypass",
port = "5432")
一切正常。
更奇怪的是,当 shiny 的端口设置为某个值时,例如:shiny::runApp(port = 4000)
,连接建立时没有指定密码,但仅限于第一次 - 这意味着当应用程序关闭并且在同一 R 会话中重新打开,错误再次发生。
我已经测试了软件包 'RPostgreSQL' - 它也不起作用,只有错误消息不同:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect postgres@localhost on dbname "dbname")
我使用 32 位 R,但我已经在 64 位上测试过它,结果是一样的。闪亮的应用程序在浏览器 (Chrome) 和 Rstudio Viewer 中都是 运行。
这是我的会话信息:
R version 3.2.2 (2015-08-14)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Polish_Poland.1250 LC_CTYPE=Polish_Poland.1250 LC_MONETARY=Polish_Poland.1250
[4] LC_NUMERIC=C LC_TIME=Polish_Poland.1250
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RPostgres_0.1 DBI_0.3.1.9008 shiny_0.12.2
loaded via a namespace (and not attached):
[1] R6_2.1.1 htmltools_0.2.6 tools_3.2.2 rstudioapi_0.3.1 Rcpp_0.12.1.3 jsonlite_0.9.17 digest_0.6.8
[8] xtable_1.7-4 httpuv_1.3.3 mime_0.4 RPostgreSQL_0.4
Shiny 和您的系统 R GUI 之间的命令 运行 环境可能有所不同。我通过将我的凭据存储在 Renviron file:
中来解决这个问题readRenviron("~/.Renviron")
con <- dbConnect(RPostgres::Postgres(),
dbname = Sys.getenv('pg_db'),
user = Sys.getenv('api_user'),
...)
关键是您可以为暂存和生产环境维护单独的 Renviron。这允许您的脚本使用 commandArgs()
来指定它应该使用的数据库凭据:
#!/usr/bin/env Rscript
environ_path <- switch(commandArgs(),
'staging' = {"~/staging.Renviron"},
'production' = {"~/production/Renviron"})
readRenviron(environ_path)
然后从BASH:
Rscript analysis.R staging
错误在 Postgresql 中:
C:\Users\...\AppData\Roaming/postgresql/pgpass.conf
文件路径包含“/”而不是“\”