代码之家  ›  专栏  ›  技术社区  ›  string

如何在R中访问和读取Postgres视图

  •  1
  • string  · 技术社区  · 7 年前

    我试图访问和读取R中Postgres数据库的表和视图。我能够使用 dbListTables 函数使用 RPostgres 但面临的问题 views .

    由于对博士后的知识还很幼稚,所以在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));  
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Ramana    6 年前

    确保视图名称在引号中。

    注意:视图名称“viewName”在引号中。否则它不会起作用。

        2
  •  0
  •   Scarabee    7 年前

    看来 dbExistsTable dbListTables

    但您应该能够通过这样的查询找到它们:

    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