如何访问和阅读R中的Postgres视图
问题描述:
我正在尝试访问并阅读R中Postgres数据库的表和视图。我能够使用dbListTables
函数使用RPostgres
程序包获取表格,但面临的问题是views
。如何访问和阅读R中的Postgres视图
作为具有的Postgres天真的知识,寻找方式R.
library(RPostgres)
library(DBI)
library(dplyr)
library(sqldf)
pw<- {
"password"
}
conn <- dbConnect(RPostgres::Postgres()
, host="host-name"
, port='5432'
, dbname="database-name"
, user="username"
, password=pw)
dbExistsTable(conn, "Test_Table")
#TRUE
dbListTables(conn)
mydf <- dbReadTable(conn, "Test_Table") # To Read the table in R
访问和读取的意见,以及我自己也尝试下面的命令,按照该链接:https://github.com/tidyverse/dplyr/issues/1007,但没有成功。
SELECT table_name
FROM INFORMATION_SCHEMA.tables
WHERE table_schema = ANY (current_schemas(false));
答
似乎dbExistsTable
和dbListTables
无法看到Postgres的意见。
但是,你应该能够像这样的查询,以找到他们:
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_schema not in ('pg_catalog', 'information_schema') and table_type = 'VIEW'
一旦你知道你要找的,dbReadTable(conn, "myview")
工程视图的名称。
注:如果仍然不能正常工作,请务必将使用
SET search_path to myschema
感谢您回应的权利架构。我试过你建议的sql查询。令人惊讶的是,它没有。只是这个愚蠢的问题,你提出的问题来自R Side?此外,用于View的dbReadTable(conn,“vw_activeIngredient”)也会给出错误:result_create(conn @ ptr,statement)中的错误: 错误:关系“vw_activeIngredientBeta”不存在 LINE 1:SELECT * FROM“vw_activeIngredient” – string
您可以或者在pgAdmin这样的工具中使用上述查询,或者使用'dbGetQuery(conn,“查询”text“)从R中使用上述查询。如果它仍然不起作用,你得到的错误信息是什么? – Scarabee
dbGetQuery部分工作完美,但读取视图仍然给出错误dbReadTable(conn,“vw_country”)result_create(conn @ ptr,语句)中的错误: 错误:关系“vw_country”不存在 LINE 1:SELECT * FROM“vw_country “ – string