1

我无法连接到 Rstudio 中的 Postgres,如何使用 PL/R 连接 Rstudio 中的 Postgres?

下面是我用于使用 R 连接到 Postgres 的代码片段:

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))
4

0 回答 0