Re: Describe Table - Mailing list pgsql-sql

From Colin Wetherbee
Subject Re: Describe Table
Date
Msg-id 4766B323.6030102@denterprises.org
Whole thread Raw
In response to Describe Table  (<danap@dandymadeproductions.com>)
List pgsql-sql
danap@dandymadeproductions.com wrote:
> I've reviewed much of the documentation and the forums,
> but unable to seem to find a easy way to obtain the same
> thing as the 'psql \d table' through sql. I know I can create
> through collecting info on table, but seems there must be an
> easier way. I desire to create a standard type SQL dump
> syntax.

Briefly, you use the special pg_ tables [0].

The following query is probably not the most efficient way of doing it, 
but it shows the column names for the "wines" table.  The first seven 
listed are system columns (tableoid - ctid), and the rest are data 
columns (name - score).

You can look at the descriptions for each of the pg_ tables to refine 
your query a bit, exclude system columns, figure out data types, and so 
forth.

cww=# SELECT pg_class.relname, attname FROM pg_attribute, pg_class WHERE 
attrelid = pg_class.reltype::integer - 1 AND pg_class.relname = 'wines'; relname |     attname
---------+----------------- wines   | tableoid wines   | cmax wines   | xmax wines   | cmin wines   | xmin wines   |
oidwines   | ctid wines   | name wines   | vintage wines   | origin wines   | specific_origin wines   | color wines   |
typewines   | description wines   | vintner wines   | entry_date wines   | score
 
(17 rows)

This query works on 8.1.9.

Colin

[0] 
http://www.postgresql.org/files/documentation/books/aw_pgsql/node183.html


pgsql-sql by date:

Previous
From: Erik Jones
Date:
Subject: Re: Describe Table
Next
From: Richard Broersma Jr
Date:
Subject: Re: Describe Table