Reputation: 11
I am unable to connect to Postgres in Rstudio, how can I connect Postgres in Rstudio using PL/R?
Below is the code snippet I used for connecting to Postgres using R:
library(RPostgreSQL)
start_time <- Sys.time()
#************db connection to postgre**********************#
dsn_database = "postgres"
dsn_hostname = "localhost"
dsn_port = "5432"
dsn_uid = "postgres"
dsn_pwd = "tiger"
tryCatch({
drv <- dbDriver("PostgreSQL")
print("Connecting to database")
conn <- dbConnect(drv,
dbname = dsn_database,
host = dsn_hostname,
port = dsn_port,
user = dsn_uid,
password = dsn_pwd)
print("Connected!")
},
error=function(cond) {
print(cond)
print("Unable to connect to database.")
})
#************reading row count from postgr table***********#
print("tgt_count")
tgt_count <- dbGetQuery(conn, "SELECT * from COMPANY")
print(paste0("Current working dir: ", tgt_count))
Upvotes: 1
Views: 88