\d output to a file

Поиск
Список
Период
Сортировка
Искать

\d output to a file

От:
sarlav kumar <sarlavk@yahoo.com>
Дата:
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 200 tables 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 for each table manually and write the output to a file. Is there a command/ way in which I can achieve this without having to do it for each table?
Any help in this regard would be really appreciated.
 
Thanks,
Saranya

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Re: \d output to a file

От:
Gary Cowell <gary.cowell@gmail.com>
Дата:
On Wed, 15 Dec 2004 06:38:22 -0800 (PST), sarlav kumar
 wrote:
> 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 200 tables 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 for each table manually and write the output to a file.
> Is there a command/ way in which I can achieve this without having to do it
> for each table? 
> Any help in this regard would be really appreciated. 
>   
> Thanks, 
> Saranya
> 
> 

Try something like:

psql -c "\d *" >listing.txt

Re: [NOVICE] \d output to a file

От:
Geoffrey <esoteric@3times25.net>
Дата:
sarlav kumar wrote:
> 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 200 tables 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 for each table manually and write
> the output to a file. Is there a command/ way in which I can achieve
> this without having to do it for each table? Any help in this regard
> would be really appreciated.

What is the OS?  On any UNIX variant you can do:

echo '\d' | psql > outputfile

But this will get you the system tables as well I think.

Alternately you could do something like:

for table in $( outputfile

-- 
Until later, Geoffrey

Re: \d output to a file

От:
Grega Bremec <gregab@p0f.net>
Дата:
Вложения:
...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 200 tables 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 for each table manually and write the output to a file. Is there a command/ way in which I can achieve this without having to 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:

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

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

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

    $ ./describe.sh 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
used.

Hope this helped,
-- 
    Grega Bremec
    gregab at p0f dot net

Re: [NOVICE] \d output to a file

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Geoffrey  writes:
> sarlav kumar wrote:
>> I would like to write the output of the \d command on all tables in a
>> database to an output file.

> What is the OS?  On any UNIX variant you can do:
> echo '\d' | psql > outputfile

Or use \o:

regression=# \o zzz1
regression=# \d
regression=# \o
regression=# \d
             List of relations
 Schema |     Name      | Type  |  Owner
--------+---------------+-------+----------
 public | pg_ts_cfg     | table | postgres
 public | pg_ts_cfgmap  | table | postgres
 public | pg_ts_dict    | table | postgres
 public | pg_ts_parser  | table | postgres
 public | t_test        | table | postgres
 public | test_tsvector | table | postgres
(6 rows)

regression=# \q
$ cat zzz1
             List of relations
 Schema |     Name      | Type  |  Owner
--------+---------------+-------+----------
 public | pg_ts_cfg     | table | postgres
 public | pg_ts_cfgmap  | table | postgres
 public | pg_ts_dict    | table | postgres
 public | pg_ts_parser  | table | postgres
 public | t_test        | table | postgres
 public | test_tsvector | table | postgres
(6 rows)

$

			regards, tom lane

Re: [NOVICE] \d output to a file

От:
Ragnar Hafstað <gnari@simnet.is>
Дата:
On Wed, 2004-12-15 at 11:50 -0500, Tom Lane wrote:
> Geoffrey  writes:
> > sarlav kumar wrote:
> >> I would like to write the output of the \d command on all tables in a
> >> database to an output file.
> 
> > What is the OS?  On any UNIX variant you can do:
> > echo '\d' | psql > outputfile
> 
> Or use \o:
> 
> regression=# \o zzz1
> regression=# \d
or:
            =# \d *
to get all tables as th OP wanted

> regression=# \o

gnari


FAQ