Loading CODE/221104_JB_ConnectWithPSQL_publish.R 0 → 100644 +38 −0 Original line number Diff line number Diff line library("RPostgreSQL") dsn_database <- "aidaho" # Specify the name of your Database dsn_hostname <- "127.0.0.1" # localhost = 127.0.0.1 dsn_port <- "5432" # Specify your port number. e.g. 98939 dsn_uid <- "aidaho" # Specify your username. e.g. "admin" dsn_pwd <- "aidaho" # Specify your password. e.g. "xxx" tryCatch({ drv <- dbDriver("PostgreSQL") print("Connecting to Database…") connect <- dbConnect(drv, dbname = dsn_database, host = dsn_hostname, port = dsn_port, user = dsn_uid, password = dsn_pwd) print("Database Connected!") }, error=function(cond) { print("Unable to connect to Database.") } ) res <- dbSendQuery(connect,"SELECT version();") dbFetch(res, n = -1) options(digits.secs=6) test <- dbSendQuery(connect,"SELECT '2020-01-01 00:00:00.55555555'::timestamp(6) as datetime;") (data <- dbFetch(test, n = -1,format="%Y-%m-%d %H:%M:%OS6")) strptime(data$datetime,format="%Y-%m-%d %H:%M:%OS") dbListTables(connect) q <- paste0("SELECT ", "'yes'::CHAR(5);") res <- dbSendQuery(connect,q) (data <- dbFetch(res, n = -1)) No newline at end of file CODE/221206_JB_Setup_Database.Rdeleted 100644 → 0 +0 −62 Original line number Diff line number Diff line #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # 1. PRAEAMBLE ---- #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ## a. CLEAN WORKSPACE AND LOAD LIBRARIES ---- #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ rm(list=ls()) options(stringsAsFactors = FALSE) #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ## b. FUNCTIONS AND OTHER USEFUL DEFINITIONS ---- #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ `%!in%` <- Negate(`%in%`) `%ni%` <- Negate(`%in%`) library("RPostgreSQL") library("data.table") library("sqldf") dsn_database <- "aidaho" # Specify the name of your Database dsn_hostname <- "193.196.53.49" # localhost = 127.0.0.1 dsn_port <- "8001" # Specify your port number. e.g. 98939 #ADMIN USER is: dsn_uid <- "<The secret admin user>" # Specify your username. e.g. "admin" # ADMIN PASSWORD is: dsn_pwd <- "<The secret admin password>" # Specify your password. e.g. "xxx" path <- getwd() datapath <- paste0(path,"/DATA/") tryCatch({ drv <- dbDriver("PostgreSQL") print("Connecting to Database…") connect <- dbConnect(drv, dbname = dsn_database, host = dsn_hostname, port = dsn_port, user = dsn_uid, password = dsn_pwd) print("Database Connected!") }, error=function(cond) { print("Unable to connect to Database.") } ) res <- dbSendQuery(connect,"SELECT version();") dbFetch(res, n = -1) trade_report <- read.csv(paste0(datapath,"data_feed_20220124_20220124_IEXTP1_DEEP1.0_trade_report.csv")) setDT(trade_report) trade_report <- trade_report[-nrow(trade_report),] trade_report <- trade_report[,timestamp := as.POSIXct(timestamp,tz="UTC"),] dbWriteTable(connect,c('iex','trade_reports'),trade_report,row.names=FALSE) res <- dbSendQuery(connect,'ALTER TABLE iex.trade_reports ADD CONSTRAINT primary_key PRIMARY KEY (ordinal,timestamp,symbol);') dbFetch(res, n = -1) No newline at end of file Loading
CODE/221104_JB_ConnectWithPSQL_publish.R 0 → 100644 +38 −0 Original line number Diff line number Diff line library("RPostgreSQL") dsn_database <- "aidaho" # Specify the name of your Database dsn_hostname <- "127.0.0.1" # localhost = 127.0.0.1 dsn_port <- "5432" # Specify your port number. e.g. 98939 dsn_uid <- "aidaho" # Specify your username. e.g. "admin" dsn_pwd <- "aidaho" # Specify your password. e.g. "xxx" tryCatch({ drv <- dbDriver("PostgreSQL") print("Connecting to Database…") connect <- dbConnect(drv, dbname = dsn_database, host = dsn_hostname, port = dsn_port, user = dsn_uid, password = dsn_pwd) print("Database Connected!") }, error=function(cond) { print("Unable to connect to Database.") } ) res <- dbSendQuery(connect,"SELECT version();") dbFetch(res, n = -1) options(digits.secs=6) test <- dbSendQuery(connect,"SELECT '2020-01-01 00:00:00.55555555'::timestamp(6) as datetime;") (data <- dbFetch(test, n = -1,format="%Y-%m-%d %H:%M:%OS6")) strptime(data$datetime,format="%Y-%m-%d %H:%M:%OS") dbListTables(connect) q <- paste0("SELECT ", "'yes'::CHAR(5);") res <- dbSendQuery(connect,q) (data <- dbFetch(res, n = -1)) No newline at end of file
CODE/221206_JB_Setup_Database.Rdeleted 100644 → 0 +0 −62 Original line number Diff line number Diff line #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # 1. PRAEAMBLE ---- #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ## a. CLEAN WORKSPACE AND LOAD LIBRARIES ---- #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ rm(list=ls()) options(stringsAsFactors = FALSE) #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ## b. FUNCTIONS AND OTHER USEFUL DEFINITIONS ---- #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ `%!in%` <- Negate(`%in%`) `%ni%` <- Negate(`%in%`) library("RPostgreSQL") library("data.table") library("sqldf") dsn_database <- "aidaho" # Specify the name of your Database dsn_hostname <- "193.196.53.49" # localhost = 127.0.0.1 dsn_port <- "8001" # Specify your port number. e.g. 98939 #ADMIN USER is: dsn_uid <- "<The secret admin user>" # Specify your username. e.g. "admin" # ADMIN PASSWORD is: dsn_pwd <- "<The secret admin password>" # Specify your password. e.g. "xxx" path <- getwd() datapath <- paste0(path,"/DATA/") tryCatch({ drv <- dbDriver("PostgreSQL") print("Connecting to Database…") connect <- dbConnect(drv, dbname = dsn_database, host = dsn_hostname, port = dsn_port, user = dsn_uid, password = dsn_pwd) print("Database Connected!") }, error=function(cond) { print("Unable to connect to Database.") } ) res <- dbSendQuery(connect,"SELECT version();") dbFetch(res, n = -1) trade_report <- read.csv(paste0(datapath,"data_feed_20220124_20220124_IEXTP1_DEEP1.0_trade_report.csv")) setDT(trade_report) trade_report <- trade_report[-nrow(trade_report),] trade_report <- trade_report[,timestamp := as.POSIXct(timestamp,tz="UTC"),] dbWriteTable(connect,c('iex','trade_reports'),trade_report,row.names=FALSE) res <- dbSendQuery(connect,'ALTER TABLE iex.trade_reports ADD CONSTRAINT primary_key PRIMARY KEY (ordinal,timestamp,symbol);') dbFetch(res, n = -1) No newline at end of file