Connecting R to postgreSQL database

eri*_*324 6 sql postgresql r

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
Run Code Online (Sandbox Code Playgroud)

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

Jac*_*tat 7

与该RPostgres软件包组合使用时DBI,我取得了更好的成功,并且我知道,RPostgreSQL在一段时间内没有任何更改之后,它于5月刚刚发布了一个新版本。RPostgres很活跃

## install.packages("devtools")
#devtools::install_github("RcppCore/Rcpp")
#devtools::install_github("rstats-db/DBI")
#devtools::install_github("rstats-db/RPostgres")

library(RPostgres)
library(DBI)

pw<- {
  "password"
}

con <- dbConnect(RPostgres::Postgres()
     , host='localhost'
     , port='5432'
     , dbname='DBname'
     , user='user'
     , password=pw)


rm(pw) # removes the password

dbExistsTable(con, "test_table")
Run Code Online (Sandbox Code Playgroud)


Ven*_*kat 7

>install.packages("RPostgreSQL")
>require("RPostgreSQL")
#this completes installing packages
#now start creating connection
>con<-dbConnect(dbDriver("PostgreSQL"), dbname="dbname", host="localhost", port=5432, user="db_user",password="db_password")
#this completes creating connection
#get all the tables from connection
>dbListTables(con)
Run Code Online (Sandbox Code Playgroud)