【问题标题】:How do I list all tables in postgres, across schemas如何跨模式列出postgres中的所有表
【发布时间】:2014-07-10 13:58:28
【问题描述】:

我正在尝试列出数据库中的所有表。 \dt 没有这样做,可能是因为名称冲突。我尝试了很多命令,但是当不同架构中的两个表共享一个名称时,\dt 仅列出一个:

CREATE DATABASE tester;
\c tester
CREATE SCHEMA hid1;
CREATE SCHEMA hid2;
CREATE TABLE a (a int);
CREATE TABLE b (a int);
CREATE TABLE hid1.a (a int);
CREATE TABLE hid1.b (a int);
CREATE TABLE hid1.c (a int);
CREATE TABLE hid2.a (a int);
CREATE TABLE hid2.d (a int);
\dt
SET search_path TO public,hid1,hid2;
\dt
SET search_path TO hid1,public,hid2;
\dt
SET search_path TO hid2,hid1,public;
\dt

tester=# \dt
       List of relations
 Schema | Name | Type  | Owner
--------+------+-------+-------
 hid1   | a    | table | bob
 hid1   | b    | table | bob
 hid1   | c    | table | bob
 hid2   | d    | table | bob
(4 rows)

tester=# SET search_path TO hid2,hid1,public;
SET
tester=# \dt
       List of relations
 Schema | Name | Type  | Owner
--------+------+-------+-------
 hid1   | b    | table | bob
 hid1   | c    | table | bob
 hid2   | a    | table | bob
 hid2   | d    | table | bob
(4 rows)

看看三个表是如何被屏蔽的?我的理解是模式是名称空间。那是我要去错的地方吗?我错过了什么吗?

【问题讨论】:

    标签: postgresql database-schema command-prompt


    【解决方案1】:

    请求所有模式中的所有表*

    \dt *.*
    

    【讨论】:

      【解决方案2】:

      一种选择是查询information_schema。这为您提供了许多使用 SQL 进行过滤和排序的选项。

      select table_catalog, table_schema, table_name
      from information_schema.tables;
      

      【讨论】:

        【解决方案3】:

        你可以运行

        SELECT * FROM pg_catalog.pg_tables where schemaname="yourschemaname";
        

        【讨论】:

        • 值应该使用单引号,例如SELECT * FROM pg_catalog.pg_tables where schemaname='yourschemaname';
        猜你喜欢
        • 1970-01-01
        • 2011-01-15
        • 1970-01-01
        • 2021-11-26
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 2022-06-29
        • 2017-08-21
        相关资源
        最近更新 更多