Tuesday, March 7, 2017

Use PostgreSQL database from R

Use PostgreSQL database from R
# install.packages("RPostgreSQL")
require("RPostgreSQL")
# create a connection
# save the password that we can "hide" it as best as
# we can by collapsing it
pw <- {
  "new_user_password"
}
# loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# creates a connection to the postgres database
# note that "con" will be used later in each connection to the database
con <- dbConnect(drv, dbname = "postgres",
                 host = "localhost", port = 5432,
                 user = "openpg", password = pw)
rm(pw) # removes the password
# check for the cartable
dbExistsTable(con, "cartable")
# TRUE

Now let’s open up a database connection and insert the table.
# Create a connection to the database
library('RPostgreSQL')
## Loading required package: DBI pg = dbDriver("PostgreSQL") # Local Postgres.app database; no password by default
# Of course, you fill in your own database information here.

con = dbConnect(pg, user="ninazumel", password="",
host="localhost", port=5432, dbname="ninazumel")
# write the table into the database.
# use row.names=FALSE to prevent the query
# from adding the column 'row.names' to the table
# in the db

dbWriteTable(con,'iris',iris, row.names=FALSE)
## [1] TRUE
https://www.r-bloggers.com/getting-started-with-postgresql-in-r/
http://www.win-vector.com/blog/2016/02/using-postgresql-in-r/

No comments:

Post a Comment

NYSE:ORCL -- Oracle

26-03-11 ☁️ Oracle:杠杆的艺术 2026财年第三季度关键指标: ☁️营收增长加速(但存在一些问题):总营收达到 172 亿美元,同比增长 22%。按固定汇率计算,增长率仅为 18%,这意味着部分增长来自汇率波动,而非实际需求。 📦 积压订单持续攀升:RPO 达...