Freitag, 6. September 2013

Process PostgreSQL data in R with Ubuntu Linux 12.04

Here is the ultimate overview  for all who like to work with R in Ubuntu and who want to process data from PostgreSQL. As a requirement, I assume that you already setup your PostgreSQL database.


  1. Step: Install dependencies (PostgreSQL, JDBC-Connector for PostgreSQL and R)
    sudo apt-get install postgresql libpostgresql-jdbc-java r-base-core r-cran-rjava r-cran-dbi 
  2. Step: Open Terminal & run R with
    R
  3. Step: Install JDBC-Package (RJDBC) for R
    install.packages(c("RJDBC"))
  4. Step: Connect to your database from within R
    # Include JDBC package
    library(RJDBC)

    # Initialize JDBC PostgresSQL driver
    drv <- JDBC("org.postgresql.Driver", "/usr/share/java/postgresql-jdbc4-9.1.jar", identifier.quote="`")

    # Establish connection to database
    conn <- dbConnect(drv, "jdbc:postgresql://localhost/database", "user", "pw")
    # Run your SQL select query
    data <- dbGetQuery(conn, "select * from iris where Species='setosa'")
  5.  That's it :) 
If you want to connect a non-local database, you have to adjust your dbConnect-command.

Keine Kommentare:

Kommentar veröffentlichen