Home Play Pinja Bobbity flop

A blog about Ruby, Rails and other Tech. Mostly.

Back to blog

7th May 2007, 8:10am
Getting column names from postgresql

In Mysql getting the meta data about a table is easy:

SHOW FIELDS FROM tablename;
In Postgreql, the incantation is rather more arcane:
SELECT a.attnum, a.attname, t.typname, a.attlen, a.atttypmod, 
a.attnotnull, a.atthasdef FROM pg_class c, pg_attribute a, pg_type t 
WHERE a.attnum > 0 and a.attrelid = c.oid and c.relname = 'tablename' 
and a.atttypid = t.oid order by a.attnum;
Well, it's a little hard to remember, so it's handy to have it noted here.
Back to blog