[英]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_table
和user
您可以从\\dt
获取table
,从\\du
获取user
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.