Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in postgresqlNewConnection(drv, ...) RS-DBI driver: (could not connect postgres@local on dbname

Tags:

r

postgresql

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.

like image 700
user6058071 Avatar asked Nov 09 '22 18:11

user6058071


1 Answers

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
)
like image 87
spsaaibi Avatar answered Nov 15 '22 07:11

spsaaibi