"Rob Burne" <[EMAIL PROTECTED]> writes:
> I am trying to perform a query that will:
> 1. Return all user defined attributes from a relation.
> 2. Also return the datatypes of each relation.
> So far I can only achieve part 1 with the following:
> select attname from pg_class,pg_attribute
> where relname=relation_name and
> attrelid = pg_class.oid and
> atttypid != 26 and
> atttypid != 27 and
> atttypid != 28 and
> atttypid != 29;
That's a bad way to do it, because your query will drop user-defined
columns of type OID (among others). I'd suggest testing for attnum > 0
to get rid of system columns.
> But what do I need to add to return the datatype of each attribute. Any
> suggestions please?
Join against pg_type. For example,
select attname, typname
from pg_class c, pg_attribute a, pg_type t
where relname = relation_name and
attrelid = c.oid and
atttypid = t.oid and
attnum > 0
order by attnum;
psql does a lot of these sorts of queries for its \d features.
Try starting psql with -E so you can see what queries it issues
when you do a \d ...
regards, tom lane