PGSQL PERFORMANCE 23 RE D OUTPUT TO A FILE
From: gary.cowell@no-spam (Gary Cowell)
Subject: Re: \d output to a file
Date: Wed, 15 Dec 2004 15:12:17 +0000


On Wed, 15 Dec 2004 06:38:22 -0800 (PST), sarlav kumar <sarlavk@no-spam> 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
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

From: gregab@no-spam (Grega Bremec)
Subject: Re: \d output to a file
Date: Wed, 15 Dec 2004 16:35:58 +0100

=2E..and on Wed, Dec 15, 2004 at 06:38:22AM -0800, sarlav kumar used the ke=
yboard:
> Hi All,
> =20
> I would like to write the output of the \d command on all tables in a dat=
abase 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 th=
ere 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.
> =20

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=3D$1
MYTABLES=3D"`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,
--=20
Grega Bremec gregab at p0f dot net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFBwFnefu4IwuB3+XoRAvaxAJ0YdErCui8t2ak6NxyCxiQpxdOYeACfSz3y eznxq1PM4fn0tm1XkkJ9kck=
=NgiN -----END PGP SIGNATURE-----


From: gnari@no-spam (Ragnar =?ISO-8859-1?Q?Hafsta=F0?=)
Subject: Re: [NOVICE] \d output to a file
Date: Wed, 15 Dec 2004 18:23:43 +0000

On Wed, 2004-12-15 at 11:50 -0500, Tom Lane wrote:
> Geoffrey <esoteric@no-spam> 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
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@no-spam