0

I know \d only works for psql and I can use psql -E to see what are the actual SQL queries used to implement \d

Here is my example

\d foo_table
********* QUERY **********
SELECT c.oid,
  n.nspname,
  c.relname
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname ~ '^(foo_table)$'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3;
**************************

********* QUERY **********
SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers,        
c.relhasoids, '', c.reltablespace
FROM pg_catalog.pg_class c
 LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
WHERE c.oid = '16386'

**************************

********* QUERY **********
SELECT a.attname,
  pg_catalog.format_type(a.atttypid, a.atttypmod),
  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
   FROM pg_catalog.pg_attrdef d
   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
  a.attnotnull, a.attnum
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = '16386' AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
**************************

********* QUERY **********
SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i 
WHERE c.oid=i.inhparent AND i.inhrelid = '16386' ORDER BY inhseqno
**************************

There are multiple queries, and some of them end with ";" and some of them not. I am totally confused. What query should I use in PHP to do the same thing as "\d "?

Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59
  • possible duplicate of [How to list only attribute names of a table in PostgreSQL](http://stackoverflow.com/questions/13520859/how-to-list-only-attribute-names-of-a-table-in-postgresql) – Mark Hurd Nov 25 '12 at 12:05
  • See here http://stackoverflow.com/questions/21136065/how-to-programmatically-find-out-which-queries-the-psql-command-d-does/28226508#28226508 – rogerdpack Jan 29 '15 at 23:16

1 Answers1

1

Fortunately, I found the answer. http://www.linuxscrew.com/2009/07/03/postgresql-show-tables-show-databases-show-columns/

Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59