我正在尝试访问和读取R的Postgres数据库的表和视图。我能够使用RPostgres包使用dbListTables函数获得表,但遇到了views的问题。
因为对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)); 发布于 2017-07-19 19:20:47
似乎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发布于 2018-10-09 14:03:49
确保视图名称用引号括起来。
viewDataFrame<-dbGetQuery(con,'SELECT * FROM dh2."viewName"')
注意:视图名称"viewName“用引号括起来。否则它就不会起作用。
https://stackoverflow.com/questions/45186735
复制相似问题