postgresqlNewConnection(drv, ...) RS-DBI 驱动程序中出现错误:(无法在 dbname 上连接 postgres@local

use*_*071 7 postgresql r

我是新手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)


小智 -4

我有同样的问题 。我在安装RPostgreSQL后安装了DBI,并单独加载了DBI库。为我工作