mandag den 16. november 2015

How to list all databases with a given prefix in PostgeSQL

I recently had to delete a large number of databases in a PostgreSql database.

I found the code below very useful. It will list a sequence of SQL DROP DATABASE statements, one for each of the databases with a name starting with "database_name_starting_with_this".

The list of statements can be copied to psql a executed by pasting.

I want to warn you this can be dangerous, so go through the list of statements one by one, before you execute them.


SELECT 'DROP DATABASE "' || datname || '";' FROM pg_database
WHERE datname LIKE 'database_name_starting_with_this%';

Ingen kommentarer:

Send en kommentar

Bemærk! Kun medlemmer af denne blog kan sende kommentarer.