tags:

views:

77

answers:

5

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

Any ideas will be appreciated

+2  A: 

You can get the data from information_schema.tables.

Lukáš Lalinský
+1  A: 

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

pg_catalog usually shouldn't be touched. there is information_schema, and it's generally much better.
depesz
@depesz: That comment contradicts your `psql -E`-tip.
Alex Brasetvik
yes and no. If I was to write selects from scratch - I would use information_schema. On the other hand - psql -E shows ready SQLs. Not portable, but ready to use.
depesz
Oh shoot! I recognize depesz from #postgresql. Alex, listen to him, he knows his stuff!
+2  A: 

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;
Ewan Todd
+2  A: 

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

depesz
+1  A: 

There is also pg_dump --schema-only.

Alex Brasetvik
it hardly counts as "run sql to get table descriptions". it is: run command to see the sql that creates the table.
depesz
That's not what he wrote, and it's not like [XY-problems](http://www.perlmonks.org/index.pl?node=XY%20problem) never occur --- I did write *also*.
Alex Brasetvik