Skip to content

Instantly share code, notes, and snippets.

@00krishna
Created May 23, 2014 22:13
Show Gist options
  • Save 00krishna/19313e7dcdfde73fcbd5 to your computer and use it in GitHub Desktop.
Save 00krishna/19313e7dcdfde73fcbd5 to your computer and use it in GitHub Desktop.
connect to postgresql database
# Import Libraries
library(RPostgreSQL)
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#' Connect to PostgreSQL database
#'
#' This takes a database name and then connects to it.
#' @param dname The name of the database that houses the dlhs data
#' @return a database connection object to the specified database
#' @author krishna bhogaonker, apr 2014
#' @export
db_conn <- function(dname) {
drv <- dbDriver("PostgreSQL")
# make a connection
tryCatch({
con <- dbConnect(drv,dbname=dname)},
error=function(e) {stop(conditionMessage(e), " error connecting to the database, check that the name is correct")
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment