Skip site navigation (1) Skip section navigation (2)

Re: [PERFORM] \d output to a file

From: Grega Bremec <gregab(at)p0f(dot)net>
To: sarlav kumar <sarlavk(at)yahoo(dot)com>
Cc: pgsqlnovice <pgsql-novice(at)postgresql(dot)org>,pgsqlperform <pgsql-performance(at)postgresql(dot)org>
Subject: Re: [PERFORM] \d output to a file
Date: 2004-12-15 15:35:58
Message-ID: 20041215153558.GA7419@lunik.p0f.net (view raw or flat)
Thread:
Lists: pgsql-novicepgsql-performance
...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

In response to

pgsql-novice by date

Next:From: Kretschmer AndreasDate: 2004-12-15 16:21:57
Subject: Re: [despammed] \d output to a file
Previous:From: GeoffreyDate: 2004-12-15 15:23:54
Subject: Re: [despammed] \d output to a file

pgsql-performance by date

Next:From: Kretschmer AndreasDate: 2004-12-15 16:21:57
Subject: Re: [despammed] \d output to a file
Previous:From: GeoffreyDate: 2004-12-15 15:23:54
Subject: Re: [despammed] \d output to a file

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group