library(RPostgreSQL) # load the database connection library
drv <- dbDriver("PostgreSQL") # create instance of database driver
all_cons <- dbListConnections(drv)
for(con in all_cons)
{dbDisconnect(con)}
con <- dbConnect(drv, dbname="sampleDb", host="localhost", port=5432
, user="aaron", password="aaron")
source(file="~/R/pg_connect.R")
dbname <- dbGetQuery(con, "SELECT table_catalog FROM information_schema.tables LIMIT 1")
View(dbname)
sql = paste("SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_catalog = '", dbname, "'
AND table_schema NOT IN ('pg_catalog', 'files', 'information_schema')
ORDER BY table_schema, table_name", sep="")
dbTables <- dbGetQuery(con, sql)
source(file="pg_connect.R")
schemaName = "sample_schema"
tableName = "sample_table"
sql = paste("SELECT column_name, data_type
FROM information_schema.columns
WHERE table_schema = '",schemaName,"'
AND table_name = '",tableName,"'", sep="")
tableColumns <- dbGetQuery(con, sql)
source(file="pg_connect.R")
myTable <- dbGetQuery(con, "SELECT id, item, description
FROM sample_schema.sample_table")