Home:ALL Converter>Connecting R to postgreSQL database

Connecting R to postgreSQL database

Ask Time:2016-11-17T05:27:30         Author:erik12324

Json Formatter

I am trying to connect R to a postgreSQL database. He is what I have been trying in R:

require("RPostgreSQL")

pw<- {
  "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 = "DBname",
                 host = "localhost", port = 5432,
                 user = "user", password = pw)
rm(pw) # removes the password

# check for the test_table
dbExistsTable(con, "test_table")
# FALSE >>> Should be true

I cannot figure out why it is not properly connecting to my database. I know that the database is on my computer as I can connect to it in the terminal and with pgAdmin4. Any help is greatly appreciated.

Thanks

Author:erik12324,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40642657/connecting-r-to-postgresql-database
JackStat :

I have had better success with the RPostgres package in combination with DBI and I know that RPostgreSQL just released a new version in May after no changes for a while. RPostgres is pretty active\n\n## install.packages(\"devtools\")\n#devtools::install_github(\"RcppCore/Rcpp\")\n#devtools::install_github(\"rstats-db/DBI\")\n#devtools::install_github(\"rstats-db/RPostgres\")\n\nlibrary(RPostgres)\nlibrary(DBI)\n\npw<- {\n \"password\"\n}\n\ncon <- dbConnect(RPostgres::Postgres()\n , host='localhost'\n , port='5432'\n , dbname='DBname'\n , user='user'\n , password=pw)\n\n\nrm(pw) # removes the password\n\ndbExistsTable(con, \"test_table\")\n",
2016-11-17T05:09:48
Venkat :

install.packages("RPostgreSQL")\nrequire("RPostgreSQL")\n# this completes installing packages\n\n# now start creating connection\ncon <- dbConnect(dbDriver("PostgreSQL"),\n dbname = "dbname",\n host = "localhost",\n port = 5432,\n user = "db_user",\n password = "db_password")\n# this completes creating connection\n\n# get all the tables from connection\ndbListTables(con)\n",
2017-03-01T09:55:16
yy