pg.ddx.io  pgsql-docs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SQL query: List all the databases in the server
5+ messages / 4 participants
[nested] [flat]

* SQL query: List all the databases in the server
@ 2001-07-03 11:15 Roman Smirnov <smirnov@dresearch.de>
  2001-07-03 11:44 ` Re: SQL query: List all the databases in the server Vince Vielhaber <vev@michvhf.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Roman Smirnov @ 2001-07-03 11:15 UTC (permalink / raw)
  To: pgsql-docs

Hi all!

I need SQL analog of \l command from psql.
Something like "list databases".

Does anyone can help me?

Beforehand thank,
Roman.




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL query: List all the databases in the server
  2001-07-03 11:15 SQL query: List all the databases in the server Roman Smirnov <smirnov@dresearch.de>
@ 2001-07-03 11:44 ` Vince Vielhaber <vev@michvhf.com>
  2001-07-03 12:35   ` Re: SQL query: List all the databases in the server Tom Ivar Helbekkmo <tih@kpnQwest.no>
  0 siblings, 1 reply; 5+ messages in thread

From: Vince Vielhaber @ 2001-07-03 11:44 UTC (permalink / raw)
  To: Roman Smirnov <smirnov@dresearch.de>; +Cc: pgsql-docs

On Tue, 3 Jul 2001, Roman Smirnov wrote:

> Hi all!
>
> I need SQL analog of \l command from psql.
> Something like "list databases".

If you just want a list of them you can get it from pg_database:

select datname from pg_database;

if you also want the username of the database owner you'll have to
cross pg_database.datdba to pg_user.usesysid something like this:

select d.datname,u.usename from pg_database d, pg_user u where d.datdba = u.usesysid;

Vince.
-- 
==========================================================================
Vince Vielhaber -- KA8CSH    email: vev@michvhf.com    http://www.pop4.net
         56K Nationwide Dialup from $16.00/mo at Pop4 Networking
        Online Campground Directory    http://www.camping-usa.com
       Online Giftshop Superstore    http://www.cloudninegifts.com
==========================================================================






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL query: List all the databases in the server
  2001-07-03 11:15 SQL query: List all the databases in the server Roman Smirnov <smirnov@dresearch.de>
  2001-07-03 11:44 ` Re: SQL query: List all the databases in the server Vince Vielhaber <vev@michvhf.com>
@ 2001-07-03 12:35   ` Tom Ivar Helbekkmo <tih@kpnQwest.no>
  2001-07-03 14:45     ` Re: SQL query: List all the databases in the server Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Ivar Helbekkmo @ 2001-07-03 12:35 UTC (permalink / raw)
  To: Vince Vielhaber <vev@michvhf.com>; +Cc: Roman Smirnov <smirnov@dresearch.de>; pgsql-docs

Vince Vielhaber <vev@michvhf.com> writes:

>> I need SQL analog of \l command from psql.
>> Something like "list databases".
> 
> If you just want a list of them you can get it from pg_database:
> 
> select datname from pg_database;
> 
> if you also want the username of the database owner [...]

The psql program implements the various \-ed information commands
using SQL, and you can find the actual code by perusing its source
file "describe.c".  In this case, we find that "\l" is:

SELECT pg_database.datname as "Database",
       pg_user.usename as "Owner",
       pg_encoding_to_char(pg_database.encoding) as "Encoding",
       obj_description(pg_database.oid) as "Description"
  FROM pg_database, pg_user
 WHERE pg_database.datdba = pg_user.usesysid
UNION
SELECT pg_database.datname as "Database",
       NULL as "Owner",
       pg_encoding_to_char(pg_database.encoding) as "Encoding",
       obj_description(pg_database.oid) as "Description"
  FROM pg_database
 WHERE pg_database.datdba NOT IN (SELECT usesysid FROM pg_user)
ORDER BY "Database";

However, the "Encoding" bits are only included if the system is
compiled with support for multiple character set encodings, and the
"Description" bits only if the command is given as "\l+", which is a
new one for me -- it's not included in "\?" output.  It seems, from a
little experimentation, that that "+" suffix is available also for the
other "\" commands where it's relevant.  Cool!  :-)

The above SELECT is extensively reformatted from the strings it's
built from in the source file, of course.

-tih
-- 
The basic difference is this: hackers build things, crackers break them.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL query: List all the databases in the server
  2001-07-03 11:15 SQL query: List all the databases in the server Roman Smirnov <smirnov@dresearch.de>
  2001-07-03 11:44 ` Re: SQL query: List all the databases in the server Vince Vielhaber <vev@michvhf.com>
  2001-07-03 12:35   ` Re: SQL query: List all the databases in the server Tom Ivar Helbekkmo <tih@kpnQwest.no>
@ 2001-07-03 14:45     ` Tom Lane <tgl@sss.pgh.pa.us>
  2001-07-03 15:33       ` Re: SQL query: List all the databases in the server Roman Smirnov <smirnov@dresearch.de>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2001-07-03 14:45 UTC (permalink / raw)
  To: Tom Ivar Helbekkmo <tih@kpnQwest.no>; +Cc: Vince Vielhaber <vev@michvhf.com>; Roman Smirnov <smirnov@dresearch.de>; pgsql-docs

Tom Ivar Helbekkmo <tih@kpnQwest.no> writes:
> The above SELECT is extensively reformatted from the strings it's
> built from in the source file, of course.

An even easier way to see what queries psql emits for its backslash
commands is to start psql with -E option, then do the backslash
commands.  This gives me, for example,

regression=# \l
********* QUERY **********
SELECT pg_database.datname as "Database",
       pg_user.usename as "Owner"FROM pg_database, pg_user
WHERE pg_database.datdba = pg_user.usesysid

UNION

SELECT pg_database.datname as "Database",
       NULL as "Owner"FROM pg_database
WHERE pg_database.datdba NOT IN (SELECT usesysid FROM pg_user)
ORDER BY "Database"
**************************

   List of databases
  Database  |  Owner
------------+----------
 regression | postgres
 template0  | postgres
 template1  | postgres
(3 rows)

regression=#

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL query: List all the databases in the server
  2001-07-03 11:15 SQL query: List all the databases in the server Roman Smirnov <smirnov@dresearch.de>
  2001-07-03 11:44 ` Re: SQL query: List all the databases in the server Vince Vielhaber <vev@michvhf.com>
  2001-07-03 12:35   ` Re: SQL query: List all the databases in the server Tom Ivar Helbekkmo <tih@kpnQwest.no>
  2001-07-03 14:45     ` Re: SQL query: List all the databases in the server Tom Lane <tgl@sss.pgh.pa.us>
@ 2001-07-03 15:33       ` Roman Smirnov <smirnov@dresearch.de>
  0 siblings, 0 replies; 5+ messages in thread

From: Roman Smirnov @ 2001-07-03 15:33 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Tom Ivar Helbekkmo <tih@kpnQwest.no>; Vince Vielhaber <vev@michvhf.com>; pgsql-docs

Thanks to all!

Your help was very usefull

Thanks again,
Roman





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2001-07-03 15:33 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-07-03 11:15 SQL query: List all the databases in the server Roman Smirnov <smirnov@dresearch.de>
2001-07-03 11:44 ` Vince Vielhaber <vev@michvhf.com>
2001-07-03 12:35   ` Tom Ivar Helbekkmo <tih@kpnQwest.no>
2001-07-03 14:45     ` Tom Lane <tgl@sss.pgh.pa.us>
2001-07-03 15:33       ` Roman Smirnov <smirnov@dresearch.de>

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