connect2postgres: connect to a postgres db

Description Usage Arguments Author(s) Examples

Description

connect to a postgres db

Usage

1
connect2postgres(hostip = NA, db = NA, user = NA, p = NA, os = "linux", pgutils = c("/home/ivan/tools/jdbc", "c:/pgutils"))

Arguments

hostip
db
user
p
os
pgutils

Author(s)

ivanhanigan

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--    or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (hostip = NA, db = NA, user = NA, p = NA, os = "linux", 
    pgutils = c("/home/ivan/tools/jdbc", "c:/pgutils")) 
{
    if (is.na(hostip)) {
        hostip = readline("enter hostip: ")
    }
    if (is.na(db)) {
        db = readline("enter db: ")
    }
    if (is.na(user)) {
        user = readline("enter user: ")
    }
    if (is.na(p)) {
        pwd = readline(paste("enter password for user ", user, 
            ": ", sep = ""))
    }
    else {
        pwd <- p
    }
    if (os == "linux") {
        if (!require(RPostgreSQL)) 
            install.packages("RPostgreSQL", repos = "http://cran.csiro.au")
        require(RPostgreSQL)
        con <- dbConnect(PostgreSQL(), host = hostip, user = user, 
            password = pwd, dbname = db)
    }
    else {
        if (!require(RJDBC)) 
            install.packages("RJDBC")
        require(RJDBC)
        if (!file.exists(file.path(pgutils, "postgresql-8.4-701.jdbc4.jar"))) {
            dir.create(pgutils, recursive = T)
            download.file("http://jdbc.postgresql.org/download/postgresql-8.4-701.jdbc4.jar", 
                file.path(pgutils, "postgresql-8.4-701.jdbc4.jar"), 
                mode = "wb")
        }
        pgsql <- JDBC("org.postgresql.Driver", file.path(pgutils, 
            "postgresql-8.4-701.jdbc4.jar"))
        con <- dbConnect(pgsql, paste("jdbc:postgresql://", hostip, 
            "/", db, sep = ""), user = user, password = pwd)
    }
    rm(pwd)
    return(con)
  }

swish-climate-impact-assessment/rpostgrestools documentation built on May 30, 2019, 10:39 p.m.