将Rstudio连接到PostgresSQL

时间:2019-02-20 11:23:34

标签: rstudio dbi rpostgresql

我正在尝试使用DBI和RPostgres从Rstudio连接到外部数据库(PostgresSQL)。我定义了所有参数:

con <- DBI::dbConnect(RPostgres::Postgres(),
                      dbname = 'name', 
                      host = 'http://bi-warehouse.cngdka9w0zww.us-east-1.rds.amazonaws.com/',
                      port = 5432,
                      user = 'user',
                      password = 'passwd')

但仍然出现错误:

    Error in connection_create(names(opts), as.vector(opts)) : 
  could not translate host name "http://bi-warehouse.cngdka9w0zww.us-east-1.rds.amazonaws.com/" to address: Name or service not known

2 个答案:

答案 0 :(得分:0)

  

试用软件包RPostgreSQL

示例:-

library(RPostgreSQL)
library(dplyr)
library(dbplyr)
psql <- DBI::dbDriver("PostgreSQL")
con <- DBI::dbConnect(psql, 
                      dbname = "machine_db",
                      host = "192.168.13.213", 
                      port = 5432,
                      user = "user", 
                      password = 'user123')

machine_data <- dbGetQuery(con, "SELECT * from machine_db")

答案 1 :(得分:0)

使用RPostgres软件包

更多信息-https://github.com/r-dbi/RPostgres

library(DBI)
library(RPostgres)

conn <- dbConnect(RPostgres::Postgres(),
                 host = "xyz-db-postgresql.postgres.database.aws.com",
                 port = 5432,
                 dbname = "xyz",
                 user = "xyz",
                 password = "password"
)

#==== Top 100 Records Of CommodityTable ====
 qr<- paste0('SELECT * from "Cxtable " LIMIT 100')
 Cxtable <- dbGetQuery(conn, qr)
 Cxtable