繁体   English   中英

将 R 连接到 postgreSQL 数据库

[英]Connecting R to postgreSQL database

我正在尝试将 R 连接到 postgreSQL 数据库。 他就是我在 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

我无法弄清楚为什么它没有正确连接到我的数据库。 我知道数据库在我的电脑上,因为我可以在终端和 pgAdmin4 中连接到它。 非常感谢任何帮助。

谢谢

我将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")
>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)

一个问题可能是表权限

GRANT ALL PRIVILEGES ON your_table TO user

用您自己的凭据替换your_tableuser

您可以从\\dt获取table ,从\\du获取user

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM