我无法连接到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))