Skip to content

Instantly share code, notes, and snippets.

@hernamesbarbara
Created July 6, 2012 13:11
Show Gist options
  • Save hernamesbarbara/3060079 to your computer and use it in GitHub Desktop.
Save hernamesbarbara/3060079 to your computer and use it in GitHub Desktop.
how to connect to a postgreSQL database from R
library(RPostgreSQL) #import the library
driver <- dbDriver('PostgreSQL') #define the database driver
db <- dbConnect(driver, host='foobar_host', #set the host to the postgreSQL IP or alias
user='foo_bar', #input username
password='my_secret_code', #input your password
dbname='my_gp_db') #input the name of the databse
myquery <- "SELECT
col_one
,col_two
,col_three
FROM table_one a
INNER JOIN table_two b
ON a.id = b.a_id
WHERE
a.created_date > 2012
LIMIT 100;"
my.data <- dbGetQuery(db, myquery) #assign results to a vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment