Connect R to a Postgresql database

I have got information in R which should be stored. Also querying and reloading the data should be possible.

Taggings:

1 answer

Using RPostgresql package

1. The package must be loaded into the workspace
install.packages("RPostgreSQL")
library(RPostgreSQL)

2. A driver must be loaded
drv <- dbDriver("PostgreSQL")

3. Create a connection to a database
con <- dbConnect(drv, host="localhost",port="5432", user="postgres", dbname="test", password="blub")

4. Submits a statement (this can be also a insert, drop or update SQL statment)
rs <- dbSendQuery(con, "select * from Test")

5. Fetch all elements from the result set
fetch(rs,n=-1)

## Closes the connection
dbDisconnect(con)
## Frees all the resources on the driver
dbUnloadDriver(drv)

Taggings: