我需要使用Node获取特定Postgres数据库中的所有表格,但找不到任何实现的方法。有没有办法实现这个目标?
例如,假设我有一个名为“TestDatabase”的数据库,其中包含4个表(只是假设可能会有更少或更多):Person、Company、Clothes和Animal。我需要使用Node获取它们的名称。
我还在使用Node-Postgres('pg')连接到数据库。
我需要使用Node获取特定Postgres数据库中的所有表格,但找不到任何实现的方法。有没有办法实现这个目标?
例如,假设我有一个名为“TestDatabase”的数据库,其中包含4个表(只是假设可能会有更少或更多):Person、Company、Clothes和Animal。我需要使用Node获取它们的名称。
我还在使用Node-Postgres('pg')连接到数据库。
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')
您可以进一步修改它,只包含您需要的模式。
SELECT
relname
FROM
pg_class
WHERE
relkind = 'r';
pg_class
是系统目录,存储有关类似表的对象的信息。因此需要将relkind
限制为“r”。这将包括所有表类型。要进一步限制,请参见下面链接中的relpersistence
。
https://www.postgresql.org/docs/current/catalog-pg-class.html