A blog about Ruby, Rails and other Tech. Mostly.
Back to blog
Getting column names from postgresqlIn 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 |