From: Tom Ivar Helbekkmo <tih@kpnQwest.no>
To: Vince Vielhaber <vev@michvhf.com>
Cc: Roman Smirnov <smirnov@dresearch.de>
Cc: PostgreSQL conferention <pgsql-docs@postgresql.org>
Subject: Re: SQL query: List all the databases in the server
Date: 03 Jul 2001 14:35:28 +0200
Message-ID: <86zoamdwbz.fsf@athene.i.eunet.no> (raw)
In-Reply-To: <Pine.BSF.4.30.0107030740100.56876-100000@paprika.michvhf.com>
References: <Pine.BSF.4.30.0107030740100.56876-100000@paprika.michvhf.com>
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.
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-docs@postgresql.org
Cc: tih@kpnQwest.no, vev@michvhf.com, smirnov@dresearch.de
Subject: Re: SQL query: List all the databases in the server
In-Reply-To: <86zoamdwbz.fsf@athene.i.eunet.no>
* 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