Re: databases list to file

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: postgres Emanuel CALVO FRANCO <postgres(dot)arg(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: databases list to file
Date: 2008-10-14 19:18:55
Message-ID: 48F4F09F.5000902@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

postgres Emanuel CALVO FRANCO wrote:
> inside psql turn \o file
> then \l
>
>
>
>
>
>>> I need to print to a file a simple list of all the databases on my
>>> postgresql.
>>>
>>> I need to do this from a shell script to be executed without human
>>> intervention...
>>> ...
>>> but I don't know exactly to what extent I can format the output of this
>>> in order to simplify the parser that will read that file.
>>>
>>>
>> psql -tc 'select datname from pg_database' template1
>>
>>
>>
psql ...connection options... --no-align --tuples-only -l
steve|steve|UTF8
foo|steve|UTF8
template0|postgres|UTF8
template1|postgres|UTF8

If you only want the names and use the query suggested above, you can
add "where not datistemplate" to eliminate template0 and template1 from
your list.

You can format at will in-line using heredoc format:
psql <<EOS | your_output_parser
\pset format unaligned
\pset tuples_only
\pset fieldsep '\t'
...
select ...
EOS

(Note: in scripts I usually run psql with the --no-psqlrc option and set
the options myself so the script will be portable regardless of the
contents of the .psqlrc file).

Use the -E option to psql to see the queries it runs behind-the-scenes
in response to the various \l, \d, etc. commands then modify the query
to suit.

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Steve Atkins 2008-10-14 19:23:52 Re: Update with a Repeating Sequence
Previous Message Magnus Hagander 2008-10-14 19:17:39 Re: Drupal and PostgreSQL - performance issues?