Re: [PERFORM] \d output to a file

От: Grega Bremec
Тема: Re: [PERFORM] \d output to a file
Дата: ,
(см: обсуждение, исходный текст)
Ответ на: \d output to a file  (sarlav kumar)
Список: pgsql-novice

Скрыть дерево обсуждения

\d output to a file  (sarlav kumar, )
 Re: [despammed] \d output to a file  (Andreas Kretschmer, )
  Re: [despammed] \d output to a file  (Geoffrey, )
   Re: [despammed] \d output to a file  (Kretschmer Andreas, )
 Re: \d output to a file  (Geoffrey, )
  Re: \d output to a file  (Tom Lane, )
   Re: \d output to a file  (Kretschmer Andreas, )
   Re: [PERFORM] \d output to a file  (Ragnar Hafstað, )
 Re: [PERFORM] \d output to a file  (Gary Cowell, )
 Re: [PERFORM] \d output to a file  (Grega Bremec, )

...and on Wed, Dec 15, 2004 at 06:38:22AM -0800, sarlav kumar used the keyboard:
> Hi All,
> I would like to write the output of the \d command on all tables in a database to an output file. There are more than
200tables in the database. I am aware of \o command to write the output to a file. But, it will be tough to do the \d
foreach table manually and write the output to a file. Is there a command/ way in which I can achieve this without
havingto do it for each table? 
> Any help in this regard would be really appreciated.

Hello Sarlav.

You don't say which platform you're doing this on. If it's Windows, someone
else will have to advise you; if it's a UNIX-like platform though, the
following simple shell script should be helpful in achieving what you want:

if [ -z "$1" ]; then
    echo "Please specify a database to query."
    exit 1
MYTABLES="`echo '\t\a\dt' | psql -q ${DATABASE} | cut -f 2 -d '|'`"

for table in ${MYTABLES}; do
    echo '\d '${table}
done | psql ${DATABASE}

You can store this script into a file called, for example, and
invoke it like so:

    $ ./ mydatabase > description.txt

It should then do what you want.

Should you have additional arguments to specify to psql, such as a host,
a username, a password and so on, it is easy to modify the script to do
that. Just supply those arguments in places where the "psql" command is

Hope this helped,
    Grega Bremec
    gregab at p0f dot net


В списке pgsql-novice по дате сообщения:

От: George Weaver
Сообщение: Re: how to echo SQL commands in the output of a script
От: Tom Lane
Сообщение: Re: Copying pg_control file