Re: Describe Table

Lists: pgsql-sql
From: <danap(at)dandymadeproductions(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Describe Table
Date: 2007-12-17 16:56:32
Message-ID: 20071217165211.E18222E3202@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

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.

ex.
DROP TABLE IF EXISTS postgresqltypes;
CREATE TABLE postgresqltypes (

-- Table id and creation data entries.

data_type_id serial NOT NULL,
smallInt_type smallint DEFAULT NULL,
int_type integer DEFAULT NULL,
bigInt_type bigint DEFAULT NULL,
decimal_type decimal(16,2) DEFAULT NULL,
numeric_type numeric(10,2) DEFAULT NULL,
real_type real DEFAULT NULL,
doublePrecision_type double precision DEFAULT NULL,
serial_type serial,
bigSerial_type bigserial,
PRIMARY KEY (data_type_id));

dana.


From: Erik Jones <erik(at)myemma(dot)com>
To: danap(at)dandymadeproductions(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Describe Table
Date: 2007-12-17 17:33:38
Message-ID: AB0DA566-22CA-4D99-B743-F87516E70FDD@myemma.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql


On Dec 17, 2007, at 10:56 AM, <danap(at)dandymadeproductions(dot)com>
<danap(at)dandymadeproductions(dot)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.

If you start psql with the -E flag, it will display all sql generated
by internal commands such as those generated by \d commands.

Erik Jones

Software Developer | Emma®
erik(at)myemma(dot)com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


From: Colin Wetherbee <cww(at)denterprises(dot)org>
To: danap(at)dandymadeproductions(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Describe Table
Date: 2007-12-17 17:34:27
Message-ID: 4766B323.6030102@denterprises.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

danap(at)dandymadeproductions(dot)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 | oid
wines | ctid
wines | name
wines | vintage
wines | origin
wines | specific_origin
wines | color
wines | type
wines | 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


From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org, danap(at)dandymadeproductions(dot)com
Subject: Re: Describe Table
Date: 2007-12-17 17:47:22
Message-ID: 514977.80789.qm@web31801.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

--- On Mon, 12/17/07, danap(at)dandymadeproductions(dot)com

> 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

I've never understood the information schema well enough to construct my own sql queries that provide the same useful information as 'psql \d'. So instead, I cheat:

start psql with the '-E' option to echo all sql strings psql sends to the back-end server.

Simply copy and tailor each statement to suit your needs.

Regards,
Richard Broersma Jr.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Erik Jones <erik(at)myemma(dot)com>
Cc: danap(at)dandymadeproductions(dot)com, pgsql-sql(at)postgresql(dot)org
Subject: Re: Describe Table
Date: 2007-12-17 18:03:20
Message-ID: 22677.1197914600@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Erik Jones <erik(at)myemma(dot)com> writes:
> <danap(at)dandymadeproductions(dot)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.

> If you start psql with the -E flag, it will display all sql generated
> by internal commands such as those generated by \d commands.

Another alternative is to run "pg_dump -s -t <table>"; that will get you
a lot closer to SQL-ready output, and you won't need to worry as much
about updating your code for future system catalog changes.

regards, tom lane