Unix Technical Forum

Show tables query

This is a discussion on Show tables query within the pgsql Admins forums, part of the PostgreSQL category; --> Hi, Is there an SQL command supported by Postgres to return a list of tables in a database? For ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 06:58 AM
Andy Shellam
 
Posts: n/a
Default Show tables query

Hi,

Is there an SQL command supported by Postgres to return a list of tables in
a database?

For example on mySQL, you can connect to a database and issue the command
SHOW TABLES to bring back a list of tables in that database.
In PG this throws the error "unknown configuration parameter TABLES."

Any ideas?

Thanks

Andy

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 06:58 AM
Grega Bremec
 
Posts: n/a
Default Re: Show tables query

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Andy Shellam wrote:
| Hi,
|
| Is there an SQL command supported by Postgres to return a list of
| tables in a database?
|
| For example on mySQL, you can connect to a database and issue the
| command SHOW TABLES to bring back a list of tables in that database.
| In PG this throws the error "unknown configuration parameter TABLES.">

Hello, Andy.

Not a command per se, but there are two ways you can obtain this
information, depending on where you're working.

The first option is the backslash commands you can use from f.e. psql
(type \? in a psql prompt to see the full list), where \d will list all
sorts of database objects, \dt can be used specifically for tables.

The other option which you can use from an SQL script is accessing the
system tables pg_class, pg_namespace and pg_tablespace in schema
pg_catalog, using a query similar to those used by the backslash commands:

~ template1=# SELECT c.relname AS table FROM pg_class c
~ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
~ WHERE n.nspname = 'public'
~ AND c.relkind = 'r';

Substitute 'public' for whatever schema you're interested in or add
other schemas according to your preference. Also, take a look at system
view pg_tables (\d pg_tables).

Also, take a look at the archives, Elein once posted a nice set of views
~ and statements you can use for such purposes.

Hope this helped,
- --
~ Grega Bremec
~ gregab at p0f dot net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFEL5qjfu4IwuB3+XoRA7CDAKCBFI7749wtFfsf4GXm3J cPQBU81gCffZMT
GbeTZzo0T3RJBwvwlK61O9c=
=NhhS
-----END PGP SIGNATURE-----

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 06:58 AM
Oleg Bartunov
 
Posts: n/a
Default Re: Show tables query

On Sun, 2 Apr 2006, Grega Bremec wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: RIPEMD160
>
> Andy Shellam wrote:
> | Hi,
> |
> | Is there an SQL command supported by Postgres to return a list of
> | tables in a database?
> |
> | For example on mySQL, you can connect to a database and issue the
> | command SHOW TABLES to bring back a list of tables in that database.
> | In PG this throws the error "unknown configuration parameter TABLES.">
>
> Hello, Andy.
>
> Not a command per se, but there are two ways you can obtain this
> information, depending on where you're working.
>
> The first option is the backslash commands you can use from f.e. psql
> (type \? in a psql prompt to see the full list), where \d will list all
> sorts of database objects, \dt can be used specifically for tables.
>
> The other option which you can use from an SQL script is accessing the
> system tables pg_class, pg_namespace and pg_tablespace in schema
> pg_catalog, using a query similar to those used by the backslash commands:
>
> ~ template1=# SELECT c.relname AS table FROM pg_class c
> ~ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
> ~ WHERE n.nspname = 'public'
> ~ AND c.relkind = 'r';
>
> Substitute 'public' for whatever schema you're interested in or add
> other schemas according to your preference. Also, take a look at system
> view pg_tables (\d pg_tables).
>
> Also, take a look at the archives, Elein once posted a nice set of views
> ~ and statements you can use for such purposes.


psql -E will show you all queries that internal commands generate


>
> Hope this helped,
> - --
> ~ Grega Bremec
> ~ gregab at p0f dot net
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.0 (GNU/Linux)
>
> iD8DBQFEL5qjfu4IwuB3+XoRA7CDAKCBFI7749wtFfsf4GXm3J cPQBU81gCffZMT
> GbeTZzo0T3RJBwvwlK61O9c=
> =NhhS
> -----END PGP SIGNATURE-----
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>


Regards,
Oleg
__________________________________________________ ___________
Oleg Bartunov, Research Scientist, Head of AstroNet (www.astronet.ru),
Sternberg Astronomical Institute, Moscow University, Russia
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(495)939-16-83, +007(495)939-23-83

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 06:58 AM
Christopher Browne
 
Posts: n/a
Default Re: Show tables query

After takin a swig o' Arrakan spice grog, andy.shellam@mailnetwork.co.uk ("Andy Shellam") belched out:
> Is there an SQL command supported by Postgres to return a list of tables in a database?


Yes, it's called SELECT.

There is a standard schema called INFORMATION_SCHEMA, which contains a
variety of relevant views.

Notably, you could request:

SELECT * FROM INFORMATION_SCHEMA.TABLES;

That has the merit of actually conforming to SQL standards...
--
output = reverse("moc.liamg" "@" "enworbbc")
http://cbbrowne.com/info/
They have finally found the most ultimately useless thing on the web...
Found at the Victoria's Secret website:
"The online shop: Text Only Listing"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 06:58 AM
Rajesh Kumar Mallah
 
Posts: n/a
Default Re: Show tables query

On 4/2/06, Christopher Browne <cbbrowne@acm.org> wrote:
>
> After takin a swig o' Arrakan spice grog, andy.shellam@mailnetwork.co.uk("Andy Shellam") belched out:
> > Is there an SQL command supported by Postgres to return a list of tables

> in a database?
>
> Yes, it's called SELECT.
>
> There is a standard schema called INFORMATION_SCHEMA, which contains a
> variety of relevant views.
>
> Notably, you could request:
>
> SELECT * FROM INFORMATION_SCHEMA.TABLES




where table_type='BASE TABLE'


;


if you need tables only otherwise it returns the Views also.

That has the merit of actually conforming to SQL standards...
> --
> output = reverse("moc.liamg" "@" "enworbbc")
> http://cbbrowne.com/info/
> They have finally found the most ultimately useless thing on the web...
> Found at the Victoria's Secret website:
> "The online shop: Text Only Listing"
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-10-2008, 06:58 AM
Andy Shellam
 
Posts: n/a
Default Re: Show tables query

> After takin a swig o' Arrakan spice grog, andy.shellam@mailnetwork.co.uk
("Andy Shellam") belched out:
>> Is there an SQL command supported by Postgres to return a list of tables

in a database?

Sorry, did I say something wrong? I thought it was a perfectly valid
question actually. The application in mind is going to be run exclusively
on Postgres, so I'm not overly fussed over standards - I just wanted a quick
win, of which Grega's SQL gave it me perfectly - tables only, nothing else
included.



---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-10-2008, 06:58 AM
Rajesh Kumar Mallah
 
Posts: n/a
Default Re: Show tables query

On 4/2/06, Andy Shellam <andy.shellam@mailnetwork.co.uk> wrote:
> > After takin a swig o' Arrakan spice grog, andy.shellam@mailnetwork.co.uk

> ("Andy Shellam") belched out:
> >> Is there an SQL command supported by Postgres to return a list of tables

> in a database?
>
> Sorry, did I say something wrong? I thought it was a perfectly valid
> question actually. The application in mind is going to be run exclusively
> on Postgres,
>
> so I'm not overly fussed over standards - I just wanted a quick
> win, of which Grega's SQL gave it me perfectly - tables only, nothing else
> included.


the information_schema approach is still better than querying
the system catalogs. The system catalogs are internal to postgresql
what if future versions of postgresql change the sys catalogs dramatically ?
(your app breaks!)

information_schema is the standard which are more likely to behave
the same in all versions of pgsql becoz they are(currently) views on
the sys catalogs.

Regds
Rajesh Kumar Mallah.


> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-10-2008, 06:58 AM
Andy Shellam
 
Posts: n/a
Default Re: Show tables query

Thanks Rajesh,

That's always a risk anyway with anything - hence where upgrade testing
comes in ;-)
I'll probably go this way if I do indeed have this need still - it was only
a preliminary thought process, I just thought I'd ask the question.

Thanks

Andy

-----Original Message-----
From: pgsql-admin-owner@postgresql.org
[mailtogsql-admin-owner@postgresql.org] On Behalf Of Rajesh Kumar Mallah
Sent: Sunday, 02 April, 2006 4:32 pm
To: andy.shellam@mailnetwork.co.uk
Cc: Christopher Browne; pgsql-admin@postgresql.org
Subject: Re: [ADMIN] Show tables query

On 4/2/06, Andy Shellam <andy.shellam@mailnetwork.co.uk> wrote:
> > After takin a swig o' Arrakan spice grog, andy.shellam@mailnetwork.co.uk

> ("Andy Shellam") belched out:
> >> Is there an SQL command supported by Postgres to return a list of

tables
> in a database?
>
> Sorry, did I say something wrong? I thought it was a perfectly valid
> question actually. The application in mind is going to be run exclusively
> on Postgres,
>
> so I'm not overly fussed over standards - I just wanted a quick
> win, of which Grega's SQL gave it me perfectly - tables only, nothing else
> included.


the information_schema approach is still better than querying
the system catalogs. The system catalogs are internal to postgresql
what if future versions of postgresql change the sys catalogs dramatically
?
(your app breaks!)

information_schema is the standard which are more likely to behave
the same in all versions of pgsql becoz they are(currently) views on
the sys catalogs.

Regds
Rajesh Kumar Mallah.


> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

!DSPAM:14,442feeb335041315618668!





---------------------------(end of broadcast)---------------------------
TIP 1: 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-10-2008, 06:58 AM
Tom Lane
 
Posts: n/a
Default Re: Show tables query

"Rajesh Kumar Mallah" <mallah.rajesh@gmail.com> writes:
> information_schema is the standard which are more likely to behave
> the same in all versions of pgsql becoz they are(currently) views on
> the sys catalogs.


Unfortunately, the SQL committee did tremendous damage to that argument
by changing the definitions of some of those views in SQL2003 :-(
I'd still agree that the information_schema is less likely to change
than the underlying catalogs, but it's not an ironclad guarantee that
your app won't break.

(We haven't caught up to the SQL2003 behavior yet, but I believe Peter
Eisentraut is working on it for PG 8.2.)

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-10-2008, 06:58 AM
Peter Eisentraut
 
Posts: n/a
Default Re: Show tables query

Tom Lane wrote:
> Unfortunately, the SQL committee did tremendous damage to that
> argument by changing the definitions of some of those views in
> SQL2003 :-(


The only changes to existing views were one renamed column and two or
three cases with relaxed permission checks so that more objects are now
shown. I do not expect any application to break.

> (We haven't caught up to the SQL2003 behavior yet, but I believe
> Peter Eisentraut is working on it for PG 8.2.)


I'm committing it now. Thanks for reminding me -- I had almost
forgotten about that patch.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

---------------------------(end of broadcast)---------------------------
TIP 1: 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:04 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com