我是新手R,我正在尝试使用 RStudio 连接到 PostgreSQL。
我已经安装RPostgreSQL并尝试了以下代码:
> library("DBI", lib.loc="~/R/win-library/3.2")
> library("RPostgreSQL", lib.loc="~/R/win-library/3.2")
> con <- dbConnect(dbDriver("PostgreSQL"), dbname="Delta", user="postgres")
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect postgres@local on dbname "Delta"
Run Code Online (Sandbox Code Playgroud)
由于某种原因我无法连接到数据库。我想解决这个问题很长时间,但不知道如何解决。
小智 8
我解决这个问题的方法是使用RPostgres https://github.com/rstats-db/RPostgres。
假设您有连接url,以下代码将起作用:
library(DBI)
library(RPostgres)
con <- dbConnect(RPostgres::Postgres(),
host = url$host,
port = url$port,
dbname = url$dbname,
user = url$user,
password = url$password
)
Run Code Online (Sandbox Code Playgroud)