Saturday, October 17, 2015

Connect to PostgreSQL in R

Install RPostgreSQL from source

See here.

Connect to database in R

> library("RPostgreSQL")
> drv <- dbDriver("PostgreSQL")
> con <- dbConnect(drv, dbname="testdb")
> dbListConnections(drv)
[[1]]
<PostgreSQLConnection:(72789,0)>
> summary(con)
<PostgreSQLConnection:(72789,0)>
  User: kaiyin
  Host:
  Dbname: testdb
  No resultSet available
> company = dbGetQuery(con, "select * from company")

See more examples here.

0 comments: