#!/bin/sh
# @(#) list PostgreSQL databases
# too trivial to claim any copyright, since the query was stolen from psql(1)
# written in 2008 by Dirk Jagdmann <doj@cubic.org>

if [ -z "$PGDATABASE" ] ; then
   PGDATABASE=template1
fi

Q="SELECT d.datname as Name,
       r.rolname as Owner,
       pg_catalog.pg_encoding_to_char(d.encoding) as Encoding,
       t.spcname as Tablespace,
       pg_catalog.shobj_description(d.oid, 'pg_database') as Description
FROM pg_catalog.pg_database d
  JOIN pg_catalog.pg_roles r ON d.datdba = r.oid
  JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid
ORDER BY Name"

psql -c "$Q" "$PGDATABASE"
exit $?

=head1 NAME

listdb - list databases, owners, encoding, tablespace and description

=head1 SEE ALSO

\l command in psql(1)

=head1 AUTHOR

the "listdb" program: Dirk Jagdmann <doj@cubic.org>

the SQL query: authors of psql(1)

L<http://pgfoundry.org/projects/pg-toolbox/>

=cut
