Unknown error when trying to connect to postgresql from R with RPostgresql [duplicate]

I'm new to R and I'm trying to connect to PostgreSQL using RStudio.

I've installed the RPostgreSQL and tried the following code:

> 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"

I'm not able to connect to the database for some reason. I'm trying to solve this issue for a long time and couldn't figure out how.


My solution to this problem is to use RPostgres https://github.com/rstats-db/RPostgres.

Assuming you have a connection url, the following code will work:

library(DBI)
library(RPostgres)

con <- dbConnect(RPostgres::Postgres(),
  host = url$host,
  port = url$port,
  dbname = url$dbname,
  user = url$user,
  password = url$password
)