[英]Fetch all tables in a particuler Postgres database using node?
我需要使用 node.js 获取特定 Postgres 数据库中的所有表。 但没有找到任何方法来实现这一目标。 有没有办法得到它?
例如,假设我有一个名为“TestDatabase”的数据库,它包含 4 个表(假设它可以有更少或更多)人、公司、衣服、动物。 我需要使用 node.js 获取所有这些的名称。
我还使用 node-postgres ('pg') 连接数据库。
这是一个通用的解决方案。 使用以下查询:
SELECT
relname
FROM
pg_class
WHERE
relkind = 'r';
pg_class
是系统目录,它保存有关表等对象的信息。 因此需要将relkind
限制为 'r'。 这将包括所有表格类型。 要进一步限制,请参阅下面链接中的relpersistence
。
https://www.postgresql.org/docs/current/catalog-pg-class.html
作为替代方案,您可以使用information_schema
。 它并不比系统目录中的 pg_* 对象更好,而是标准化且更具可移植性。 这里是:
select table_schema||'.'||table_name as table_fullname
from information_schema."tables"
where table_type = 'BASE TABLE'
and table_schema not in ('pg_catalog', 'information_schema');
系统对象已被此表达式过滤
table_schema not in ('pg_catalog', 'information_schema')
您可以进一步修改它以仅包含您需要的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.