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 itpw <- {"new_user_password"}# loads the PostgreSQL driverdrv <-dbDriver("PostgreSQL")# creates a connection to the postgres database# note that "con" will be used later in each connection to the databasecon <-dbConnect(drv, dbname ="postgres",host ="localhost", port = 5432,user ="openpg", password = pw)rm(pw)# removes the password# check for the cartabledbExistsTable(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
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