How to do it...

To connect with the PostgreSQL database server, you need to know the username and password. Here is the R code to connect to the database server, create a database, and insert the data into it:

    library(RPostgreSQL)
p_word <- {
"postgres123"
}

databaseDriver <- dbDriver("PostgreSQL")
con2DB <- dbConnect(databaseDriver, dbname = "postgres",
host = "localhost", port = 5432,
user = "postgres", password = p_word)
dbExistsTable(con2DB, "airlineDB")

# Importing CSV file
dat1 <- read.csv('USAairlineData2016.csv', header = T, as.is = T)
dat1 <- dat1[c("YEAR", "QUARTER", "MONTH", "ORIGIN", "DEST",
"DEP_DELAY", "ARR_DELAY")]
dat1$ROW_ID <- 1:nrow(dat1)

# Writing the data into PostgreSQL data table "airlineDB"
dbWriteTable(con2DB, "airlineDB",
value = dat1, append = TRUE, row.names = FALSE)
dat2 <- dbGetQuery(con2DB, 'SELECT * FROM "airlineDB"')

# disconnect the connection to server
dbDisconnect(con2DB)

# disconnect driver
dbUnloadDriver(databaseDriver)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset