如何使用 R 列出表而不是 Postgres 数据库中的视图?

How can I list tables but not views from a Postgres DB, using R?

?DBI::dbListTables中我们可以读到:

This should include views and temporary objects

确实如此。

我怎样才能只看到表格,不包括视图?

如果需要的话,我正在使用驱动程序 RPostgres::Postgres()

我建议系统目录查看pg_tables for tables:

dbGetQuery(con, "SELECT * FROM pg_tables")

The manual:

The view pg_tables provides access to useful information about each table in the database.

不包含视图、实体化视图或临时 table,仅包含常规 table(包括 UNLOGGED table)。参见:

  • How to check if a table exists in a given schema

您可能想要排除系统 tables 并且只检索模式和 table 名称:

dbGetQuery(con, "SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE schemaname !~ '^pg_' AND schemaname <> 'information_schema'")

我为目录 table 添加了显式模式限定:pg_catalog.pg_tables。通常没有必要,但可以防止 search_path 设置混乱。参见:

  • How does the search_path influence identifier resolution and the "current schema"

pg_views 观看次数 - 如果您需要:

dbGetQuery(con, "SELECT * FROM pg_views")

The view pg_views provides access to useful information about each view in the database.