SQL statement : list table details

Started by Dino Hoboloneyabout 24 years ago21 messagesgeneral
Jump to latest
#1Dino Hoboloney
whatever@whatever.com

I am looking for a SQL statement which would list table names, columns,
and column types of a specific DB. So far I have managed to find a statement
that lists all of the tables and columns in the DB with

SELECT a.attnum, a.attname AS field, c.relname AS table_name FROM pg_class
c, pg_attribute a WHERE c.relname NOT LIKE 'pg%' AND relkind = 'r' AND
a.attnum > 0 AND a.attrelid = c.oid ORDER BY table_name, attnum;

unfortunately I am unable to come up with a solution to listing the data
types for the columns listed. Any ideas?

#2Neil Conway
neilc@samurai.com
In reply to: Dino Hoboloney (#1)
Re: SQL statement : list table details

On Thu, 2002-02-28 at 19:59, Samik Raychauhduri wrote:

Hi,
Thanks for this query BTW, I was looking for a query like this :)
Let's see if anybody comes up with any solution to the data types.

Do this:

$ psql -E
nconway=> \d my_table

This will display the queries that psql uses to determine the data types
of a table. The same holds true for all the other psql 'slash commands'.

Cheers,

Neil

--
Neil Conway <neilconway@rogers.com>
PGP Key ID: DB3C29FC

#3Samik Raychaudhuri
samik@cae.wisc.edu
In reply to: Dino Hoboloney (#1)
Re: SQL statement : list table details

Hi,
Thanks for this query BTW, I was looking for a query like this :)
Let's see if anybody comes up with any solution to the data types.
-samik

Dino Hoboloney wrote:

Show quoted text

I am looking for a SQL statement which would list table names, columns,
and column types of a specific DB. So far I have managed to find a statement
that lists all of the tables and columns in the DB with

SELECT a.attnum, a.attname AS field, c.relname AS table_name FROM pg_class
c, pg_attribute a WHERE c.relname NOT LIKE 'pg%' AND relkind = 'r' AND
a.attnum > 0 AND a.attrelid = c.oid ORDER BY table_name, attnum;

unfortunately I am unable to come up with a solution to listing the data
types for the columns listed. Any ideas?

#4Booth, Robert
Robert_Booth@intuit.com
In reply to: Samik Raychaudhuri (#3)
Re: SQL statement : list table details

Here are a few that may help you:

Getting database names:
SELECT datname AS database
FROM pg_database;

Getting table names for the current database:
SELECT tablename
FROM pg_tables
WHERE tablename !~* 'pg_*'

Getting the columns for a table ($table is tablename from above):
SELECT a.attnum, a.attname AS field, t.typname AS type,
a.attlen AS length, a.atttypmod AS length_var,
a.attnotnull AS not_null, a.atthasdef as has_default
FROM pg_class c, pg_attribute a, pg_type t
WHERE c.relname = '$table'
AND a.attnum > 0
AND a.attrelid = c.oid
AND a.atttypid = t.oid
ORDER BY a.attnum;

Get default value for a given field ($table is from 2 above, and $field is
from attnum above):
SELECT a.adsrc AS field_default
FROM pg_attrdef a, pg_class c
WHERE c.relname = '$table'
AND c.oid = a.adrelid
AND a.adnum = $field;

I hope this helps a bit.

Rob

-----Original Message-----
From: Samik Raychauhduri [mailto:samik@cae.wisc.edu]
Sent: Thursday, February 28, 2002 4:59 PM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] SQL statement : list table details

Hi,
Thanks for this query BTW, I was looking for a query like this :)
Let's see if anybody comes up with any solution to the data types.
-samik

Dino Hoboloney wrote:

I am looking for a SQL statement which would list table names,

columns,

and column types of a specific DB. So far I have managed to find a

statement

that lists all of the tables and columns in the DB with

SELECT a.attnum, a.attname AS field, c.relname AS table_name FROM pg_class
c, pg_attribute a WHERE c.relname NOT LIKE 'pg%' AND relkind = 'r' AND
a.attnum > 0 AND a.attrelid = c.oid ORDER BY table_name, attnum;

unfortunately I am unable to come up with a solution to listing the data
types for the columns listed. Any ideas?

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

#5Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Samik Raychaudhuri (#3)
Re: SQL statement : list table details

On Thu, 28 Feb 2002, Samik Raychauhduri wrote:

Hi,
Thanks for this query BTW, I was looking for a query like this :)
Let's see if anybody comes up with any solution to the data types.
-samik

Dino Hoboloney wrote:

I am looking for a SQL statement which would list table names, columns,
and column types of a specific DB. So far I have managed to find a statement
that lists all of the tables and columns in the DB with

SELECT a.attnum, a.attname AS field, c.relname AS table_name FROM pg_class
c, pg_attribute a WHERE c.relname NOT LIKE 'pg%' AND relkind = 'r' AND
a.attnum > 0 AND a.attrelid = c.oid ORDER BY table_name, attnum;

unfortunately I am unable to come up with a solution to listing the data
types for the columns listed. Any ideas?

If you're running 7.2, I think format_type(a.atttypid, a.atttypmod) in
the select list will give you the human readable type.

#6Marc Polatschek
Marc.Polatschek@computec.de
In reply to: Stephan Szabo (#5)
Re: SQL statement : list table details

Im using this one

SELECT pg_class.relname AS table_name,
pg_attribute.attname AS column_name,
pg_type.typname AS data_type
FROM pg_class,
pg_attribute,
pg_type
WHERE pg_class.relfilenode = pg_attribute.attrelid
AND pg_type.oid = pg_attribute.atttypid
AND pg_class.relname not like 'pg_%'
AND pg_attribute.atttypmod > 0
AND pg_class.reltype > 0
ORDER BY pg_class.relname, pg_attribute.attname

#7Dino Hoboloney
whatever@whatever.com
In reply to: Dino Hoboloney (#1)
Re: SQL statement : list table details

think I answered my own question ....

SELECT a.attnum, a.attname AS field, a.atttypid AS column_type, a.atttypmod
AS type_specific, c.relname AS table_name FROM pg_class c, pg_attribute a
WHERE c.relname NOT LIKE 'pg%' AND relkind = 'r' AND a.attnum > 0 AND
a.attrelid = c.oid ORDER BY table_name, attnum;

this will now list column_type, and type_specific which is type-specific
data such as varchar length.

#8Oliver Elphick
olly@lfix.co.uk
In reply to: Dino Hoboloney (#1)
Re: SQL statement : list table details

On Thu, 2002-02-28 at 18:57, Dino Hoboloney wrote:

I am looking for a SQL statement which would list table names, columns,
and column types of a specific DB. So far I have managed to find a statement
that lists all of the tables and columns in the DB with

SELECT a.attnum, a.attname AS field, c.relname AS table_name FROM pg_class
c, pg_attribute a WHERE c.relname NOT LIKE 'pg%' AND relkind = 'r' AND
a.attnum > 0 AND a.attrelid = c.oid ORDER BY table_name, attnum;

unfortunately I am unable to come up with a solution to listing the data
types for the columns listed. Any ideas?

SELECT a.attnum, a.attname AS field, t.typname AS type, c.relname AS
table_name FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname
NOT LIKE 'pg%' AND relkind = 'r' AND a.attnum > 0 AND a.attrelid = c.oid
AND a.atttypid = t.oid ORDER BY table_name, attnum;

If you want refinements like the number of characters in a varchar, or
the precsion and scale of a numeric, you need to do things with
a.atttypmod which I can't currently remember.

--
Oliver Elphick Oliver.Elphick@lfix.co.uk
Isle of Wight http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C

"Give, and it will be given to you. A good measure,
pressed down, taken together and running over,
will be poured into your lap. For with the same
measure that you use, it will be measured to
you." Luke 6:38

#9Jean-Luc Lachance
jllachan@nsd.ca
In reply to: Marc Polatschek (#6)
How to create crude report with psql and/or plpgsql

Hi all,

I did not find any kind of print statement in psql or plpgsql.
So, how can I create basic report with psql and/or plpgsql?
Nothing fancy, page header/footer sub-totals per page.

JLL

#10Andrew G. Hammond
drew@xyzzy.dhs.org
In reply to: Jean-Luc Lachance (#9)
Re: [SQL] How to create crude report with psql and/or plpgsql

On Tue, Mar 19, 2002 at 01:07:58PM -0500, Jean-Luc Lachance wrote:

I did not find any kind of print statement in psql or plpgsql.
So, how can I create basic report with psql and/or plpgsql?
Nothing fancy, page header/footer sub-totals per page.

Errr... psql is a database query tool. There's not much available
for doing that kind of thing. That said, why not just write a
script to do it:

#!/bin/sh
echo <<END
<html><head><title>This is a title</title></head>
<body>
<!-- header -->
END

# do the query
psql --html --command 'SELECT * FROM foo' my_dbase

echo <<END
<!-- footer -->
</body></html>
END

Of course if you really insist on doing it in psql, you could just
write SELECT statements that select a preset string. Ugly, but
it would work:

SELECT '<html><head><title>... '::text;

--
Andrew G. Hammond mailto:drew@xyzzy.dhs.org http://xyzzy.dhs.org/~drew/
56 2A 54 EF 19 C0 3B 43 72 69 5B E3 69 5B A1 1F 613-389-5481
5CD3 62B0 254B DEB1 86E0 8959 093E F70A B457 84B1
"To blow recursion you must first blow recur" -- me

#11Jean-Luc Lachance
jllachan@nsd.ca
In reply to: Marc Polatschek (#6)
Re: [SQL] How to create crude report with psql and/or plpgsql

Hello Andrew,

I do not need HTML, just plain text or maybe PDF.

It would be nice if there was one extra level for RAISE as in:
RAISE PRINT 'Whatever'
That would not send the message the log but only to the console.
One could do alot with that.

JLL

"Andrew G. Hammond" wrote:

Show quoted text

On Tue, Mar 19, 2002 at 01:07:58PM -0500, Jean-Luc Lachance wrote:

I did not find any kind of print statement in psql or plpgsql.
So, how can I create basic report with psql and/or plpgsql?
Nothing fancy, page header/footer sub-totals per page.

Errr... psql is a database query tool. There's not much available
for doing that kind of thing. That said, why not just write a
script to do it:

#!/bin/sh
echo <<END
<html><head><title>This is a title</title></head>
<body>
<!-- header -->
END

# do the query
psql --html --command 'SELECT * FROM foo' my_dbase

echo <<END
<!-- footer -->
</body></html>
END

#12Andrew G. Hammond
drew@xyzzy.dhs.org
In reply to: Jean-Luc Lachance (#11)
Re: [SQL] How to create crude report with psql and/or plpgsql

On Tue, Mar 19, 2002 at 01:33:13PM -0500, Jean-Luc Lachance wrote:

I do not need HTML, just plain text

So just do it in plain text then:

#!/bin/sh
echo <<END
This is plain text. It's not that hard.
END

psql --command 'SELECT * FROM foo' my_dbase

echo <<END
This is more plain text. Still pretty simple, eh?
END

You may also want to RTFM in the psql manual about \pset and the -f
option.

or maybe PDF.

Please don't use bad words. PDF is a proprietary format. Beware.

It would be nice if there was one extra level for RAISE as in:
RAISE PRINT 'Whatever'
That would not send the message the log but only to the console.
One could do alot with that.

Something wrong with using
SELECT 'whatever';
or didn't you even read as far as the end of my first message?

"Andrew G. Hammond" wrote:

On Tue, Mar 19, 2002 at 01:07:58PM -0500, Jean-Luc Lachance wrote:

I did not find any kind of print statement in psql or plpgsql.
So, how can I create basic report with psql and/or plpgsql?
Nothing fancy, page header/footer sub-totals per page.

Errr... psql is a database query tool. There's not much available
for doing that kind of thing. That said, why not just write a
script to do it:

#!/bin/sh
echo <<END
<html><head><title>This is a title</title></head>
<body>
<!-- header -->
END

# do the query
psql --html --command 'SELECT * FROM foo' my_dbase

echo <<END
<!-- footer -->
</body></html>
END

--
Andrew G. Hammond mailto:drew@xyzzy.dhs.org http://xyzzy.dhs.org/~drew/
56 2A 54 EF 19 C0 3B 43 72 69 5B E3 69 5B A1 1F 613-389-5481
5CD3 62B0 254B DEB1 86E0 8959 093E F70A B457 84B1
"To blow recursion you must first blow recur" -- me

#13Bruce Momjian
bruce@momjian.us
In reply to: Jean-Luc Lachance (#11)
Re: [SQL] How to create crude report with psql and/or plpgsql

Jean-Luc Lachance wrote:

Hello Andrew,

I do not need HTML, just plain text or maybe PDF.

It would be nice if there was one extra level for RAISE as in:
RAISE PRINT 'Whatever'
That would not send the message the log but only to the console.
One could do alot with that.

7.3 will have that. It will be RAISE INFO, which goes only to the
client. RAISE LOG goes only to the server logs.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#14Bruce Momjian
bruce@momjian.us
In reply to: Bruce Momjian (#13)
Re: [SQL] How to create crude report with psql and/or plpgsql

Jean-Luc Lachance wrote:

Hello Bruce,

How about simply PRINT; maybe something like a printf.

Just as long as there will be no prefix like 'INFO:' or 'NOTICE:'

Indeed, there will be a prefix if INFO: or NOTICE:. No one has asked
for those to be removed before.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#15Jean-Luc Lachance
jllachan@nsd.ca
In reply to: Bruce Momjian (#13)
Re: [SQL] How to create crude report with psql and/or plpgsql

Hello Bruce,

How about simply PRINT; maybe something like a printf.

Just as long as there will be no prefix like 'INFO:' or 'NOTICE:'

JLL

Bruce Momjian wrote:

Show quoted text

Jean-Luc Lachance wrote:

Hello Andrew,

I do not need HTML, just plain text or maybe PDF.

It would be nice if there was one extra level for RAISE as in:
RAISE PRINT 'Whatever'
That would not send the message the log but only to the console.
One could do alot with that.

7.3 will have that. It will be RAISE INFO, which goes only to the
client. RAISE LOG goes only to the server logs.

#16Jean-Luc Lachance
jllachan@nsd.ca
In reply to: Bruce Momjian (#14)
Re: [SQL] How to create crude report with psql and/or plpgsql

Well, count my vote.
Any body else who see the usefullness of a PRINT statement?

Bruce Momjian wrote:

Show quoted text

Jean-Luc Lachance wrote:

Hello Bruce,

How about simply PRINT; maybe something like a printf.

Just as long as there will be no prefix like 'INFO:' or 'NOTICE:'

Indeed, there will be a prefix if INFO: or NOTICE:. No one has asked
for those to be removed before.

--
Bruce Momjian                        |  http://candle.pha.pa.us
pgman@candle.pha.pa.us               |  (610) 853-3000
+  If your life is a hard drive,     |  830 Blythe Avenue
+  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#17Jean-Luc Lachance
jllachan@nsd.ca
In reply to: Jean-Luc Lachance (#16)
Re: [SQL] How to create crude report with psql and/or plpgsql

Wes, I Agree,

but I am thinking of psql here. PSQL is a front-end to PostgreSQL. Is it
not?
We could still use the RAISE NOTICE or RAISE INFO on the back-end, but
PSQL should strip
the INFO and/or NOTICE prefix.

Mind you, from the back-end perspective, a RAISE INFO or NOTICE is just
like a printf.

JLL

wsheldah@lexmark.com wrote:

Show quoted text

PRINT just seems like the wrong sort of thing for a database to do. If you want
printed output, use some client language like perl, java, c, whatever, to get
stuff out of the database and print it.

Or use one of those languages to write an easy filter that removes the INFO and
NOTICE prefixes before actually printing them. Use the right tool for the job.

Wes Sheldahl

Jean-Luc Lachance <jllachan%nsd.ca@interlock.lexmark.com> on 03/19/2002 02:42:52
PM

To: Bruce Momjian <pgman%candle.pha.pa.us@interlock.lexmark.com>
cc: pgsql-sql%postgresql.org@interlock.lexmark.com,
pgsql-general%postgresql.org@interlock.lexmark.com (bcc: Wesley
Sheldahl/Lex/Lexmark)
Subject: Re: [GENERAL] [SQL] How to create crude report with psql and/or
plpgsql

Well, count my vote.
Any body else who see the usefullness of a PRINT statement?

Bruce Momjian wrote:

Jean-Luc Lachance wrote:

Hello Bruce,

How about simply PRINT; maybe something like a printf.

Just as long as there will be no prefix like 'INFO:' or 'NOTICE:'

Indeed, there will be a prefix if INFO: or NOTICE:. No one has asked
for those to be removed before.

--
Bruce Momjian                        |  http://candle.pha.pa.us
pgman@candle.pha.pa.us               |  (610) 853-3000
+  If your life is a hard drive,     |  830 Blythe Avenue
+  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html

#18Roberto Mello
rmello@cc.usu.edu
In reply to: Jean-Luc Lachance (#11)
Re: How to create crude report with psql and/or plpgsql

On Tue, Mar 19, 2002 at 01:33:13PM -0500, Jean-Luc Lachance wrote:

Hello Andrew,

I do not need HTML, just plain text or maybe PDF.

It would be nice if there was one extra level for RAISE as in:
RAISE PRINT 'Whatever'
That would not send the message the log but only to the console.
One could do alot with that.

s/alot/a lot/

You could use one of the other PL languages to do that. I'm sure the PL/*U
languages do that, but perhaps the other regular PL languages would do as
well (Tcl, Python, Perl).

-Roberto

-- 
+----| http://fslc.usu.edu/ USU Free Software & GNU/Linux Club |------+
  Roberto Mello - Computer Science, USU - http://www.brasileiro.net/ 
       http://www.sdl.usu.edu/ - Space Dynamics Lab, Developer    
#19Ross J. Reedstrom
reedstrm@rice.edu
In reply to: Jean-Luc Lachance (#16)
Re: How to create crude report with psql and/or plpgsql

On Tue, Mar 19, 2002 at 02:42:52PM -0500, Jean-Luc Lachance wrote:

Well, count my vote.
Any body else who see the usefullness of a PRINT statement?

what's wrong with:

$ psql -t -c "select 'Hello World!'" template1
Hello World!

$

Show quoted text

Bruce Momjian wrote:

Jean-Luc Lachance wrote:

Hello Bruce,

How about simply PRINT; maybe something like a printf.

Just as long as there will be no prefix like 'INFO:' or 'NOTICE:'

Indeed, there will be a prefix if INFO: or NOTICE:. No one has asked
for those to be removed before.

#20Peter Eisentraut
peter_e@gmx.net
In reply to: Jean-Luc Lachance (#11)
Re: [SQL] How to create crude report with psql and/or plpgsql

Jean-Luc Lachance writes:

I do not need HTML, just plain text or maybe PDF.

psql can generate LaTeX tables, which you can convert to PDF.

--
Peter Eisentraut peter_e@gmx.net

#21Jeff Eckermann
jeff_eckermann@yahoo.com
In reply to: Jean-Luc Lachance (#9)