Re: drop table where tableName like 'backup_2007%' ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: drop table where tableName like 'backup_2007%' ?
Date: 2008-03-31 15:55:14
Message-ID: 28893.1206978914@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Emi Lu <emilu(at)encs(dot)concordia(dot)ca> writes:
> Is there a command to drop tables whose name begins a specific string?

No. The standard answer to this type of problem is to write a little
plpgsql function that scans the appropriate catalog and issues commands
constructed with EXECUTE.

for r in select relname from pg_class where relname like 'backup_2007%'
loop
execute 'DROP TABLE ' || quote_ident(r);
end loop;

Note that the above is overly simplistic --- it doesn't pay attention
to schemas, for example.

Some people prefer to just print out the constructed commands into a
file, so they can eyeball them before actually executing them.

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2008-03-31 16:27:00 Re: drop table where tableName like 'backup_2007%' ?
Previous Message Emi Lu 2008-03-31 14:48:27 drop table where tableName like 'backup_2007%' ?