Thread: drop all tables in db - without dropdb ?!

drop all tables in db - without dropdb ?!

From
Tom Tom
Date:
hi there,

i would like to drop all tables in a database without
having to issue individual drop-statements for every
table.
is there a command that can do this?

i can not use the dropdb since i do not have the
permissions :(

thnx
L



_______________________________
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
http://promotions.yahoo.com/goldrush

Re: drop all tables in db - without dropdb ?!

From
Josh Berkus
Date:
Tom tom,

> i would like to drop all tables in a database without
> having to issue individual drop-statements for every
> table.
> is there a command that can do this?

If you're usng 7.4, you can drop the public schema, which will drop all
objects in it:

DROP SCHEMA public CASCADE;

However, you may not have permission to do that either.   In that case, no,
you're stuck with one-by-one dropping.   I suggest either getting greater
permissions, or writing a quick script in Perl.

--
Josh Berkus
Aglio Database Solutions
San Francisco

Re: drop all tables in db - without dropdb ?!

From
Oliver Elphick
Date:
On Thu, 2004-08-19 at 10:16, Tom Tom wrote:
> hi there,
>
> i would like to drop all tables in a database without
> having to issue individual drop-statements for every
> table.
> is there a command that can do this?
>
> i can not use the dropdb since i do not have the
> permissions :(

You could do:

  DROP SCHEMA public CASCADE;

if you have permissions for that.  (It would delete functions and so on
as well, not just tables.)


Or you could do it in shell script:

psql -t -d my_dbname -c "SELECT 'DROP TABLE ' || n.nspname || '.' ||
c.relname || ' CASCADE;' FROM pg_catalog.pg_class AS c LEFT JOIN
pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE relkind =
'r' AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
pg_catalog.pg_table_is_visible(c.oid)" >/tmp/droptables

psql -d my_dbname -f /tmp/droptables
--
Oliver Elphick                                          olly@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
GPG: 1024D/A54310EA  92C8 39E7 280E 3631 3F0E  1EC0 5664 7A2F A543 10EA
                 ========================================
     "I saw in the night visions, and, behold, one like the
      Son of man came with the clouds of heaven, and came to
      the Ancient of days, and they brought him near before
      him. And there was given him dominion, and glory, and
      a kingdom, that all people, nations, and languages,
      should serve him; his dominion is an everlasting
      dominion, which shall not pass away, and his kingdom
      that which shall not be destroyed."
                                    Daniel 7:13,14