开发者

pg_query show/describe tables

开发者 https://www.devze.com 2022-12-14 18:07 出处:网络
Is there a way to show all tables, describe them \\dand dump the result on php? Any ideas will be appreciat开发者_如何学PythonedRun psql -E, then enter all interesting \\x commands (like \\dt, \\d ta

Is there a way to show all tables, describe them \d and dump the result on php?

Any ideas will be appreciat开发者_如何学Pythoned


Run psql -E, then enter all interesting \x commands (like \dt, \d table), and read what it will show.


Yea, Meta information about your database is stored in pg_catalog.


Here's what I see in the logs when I run \d in a psql shell:

SELECT n.nspname as "Schema",
      c.relname as "Name",
      CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type",
      r.rolname as "Owner"
    FROM pg_catalog.pg_class c
         JOIN pg_catalog.pg_roles r ON r.oid = c.relowner
         LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relkind IN ('r','v','S','')
          AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
      AND pg_catalog.pg_table_is_visible(c.oid)
    ORDER BY 1,2;


You can get the data from information_schema.tables.


There is also pg_dump --schema-only.

0

精彩评论

暂无评论...
验证码 换一张
取 消