pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Philip Hallstrom <postgresql@philip.pjkh.com>
To: Roger Tannous <roger77_lb@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: column names, types, properties for a table
Date: Wed, 7 Sep 2005 14:38:31 -0700 (PDT)
Message-ID: <20050907143715.Y22645@wolf.pjkh.com> (raw)
In-Reply-To: <20050907212543.88525.qmail@web51901.mail.yahoo.com>
References: <20050907212543.88525.qmail@web51901.mail.yahoo.com>

> Is it possible to issue an SQL query that lists column names, types (int,
> varchar, boolean, etc.), properties (like NOT NULL or UNIQUE)
> for a given table name ?

Start psql with the -E option.  Then "\d yourtable".  It will print out 
the queries that are run internally to show you the table info... for 
example:

% psql -E cc_8004
Welcome to psql 7.4.2, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
        \h for help with SQL commands
        \? for help on internal slash commands
        \g or terminate with semicolon to execute query
        \q to quit

cc_8004=# \d rep_general;
********* 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 pg_catalog.pg_table_is_visible(c.oid)
       AND c.relname ~ '^rep_general$'
ORDER BY 2, 3;
**************************

********* QUERY **********
SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules
FROM pg_catalog.pg_class WHERE oid = '21548032'
**************************

********* QUERY **********
SELECT a.attname,
   pg_catalog.format_type(a.atttypid, a.atttypmod),
   (SELECT substring(d.adsrc 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 = '21548032' AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
**************************

********* QUERY **********
SELECT c2.relname, i.indisprimary, i.indisunique, 
pg_catalog.pg_get_indexdef(i.indexrelid)
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
WHERE c.oid = '21548032' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname
**************************

********* QUERY **********
SELECT c.relname FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i 
WHERE c.oid=i.inhparent AND i.inhrelid = '21548032' ORDER BY inhseqno ASC
**************************

                          Table "public.rep_general"
          Column          |            Type             |     Modifiers
-------------------------+-----------------------------+--------------------
  id                      | integer                     | not null
  loc_id                  | integer                     | not null
  dt                      | timestamp without time zone | not null
  num_active_visits       | integer                     | not null default 0
  num_passive_visits      | integer                     | not null default 0
  min_visit_length        | integer                     | not null default 0
  max_visit_length        | integer                     | not null default 0
  total_visit_length      | integer                     | not null default 0
  total_time_before_touch | integer                     | not null default 0
  total_time_of_touch     | integer                     | not null default 0
  num_coupons_printed     | integer                     | not null default 0
  num_passive_promos      | integer                     | not null default 0
  num_active_promos       | integer                     | not null default 0
Indexes:
     "rep_general_pk" primary key, btree (id)
     "rep_general_dt_idx" btree (dt)
     "rep_general_loc_id_idx" btree (loc_id)

cc_8004=#




view thread (4+ messages)  latest in thread

Message-ID: <20050907143715.Y22645@wolf.pjkh.com>
Permalink:  ../20050907143715.Y22645@wolf.pjkh.com/
Also on:    postgresql.org/message-id/20050907143715.Y22645@wolf.pjkh.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: postgresql@philip.pjkh.com, roger77_lb@yahoo.com
  Subject: Re: column names, types, properties for a table
  In-Reply-To: <20050907143715.Y22645@wolf.pjkh.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox