Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete postgresql databases matching a pattern?

With pgAdmin III, I can list all the databases on my postgresql server.

But with pgAdmin, I can delete only 1 database at the time. It takes a lot of time to delete, for example, 30 databases, one by one...

So, what would be your approach to delete, for example, all the databases with a name containing the word "june"?

Probably I will have to build a Bash script. No problem with this. But which command would you use in this script?

I have search the web for many hours without success for this problem...

Thanks to help.

like image 259
La Page PT Avatar asked Jul 27 '16 23:07

La Page PT


People also ask

How do I force delete a database in PostgreSQL?

Using the option -f or –force with dropdb command or FORCE with DROP DATABASE to drop the database, it will terminate all existing connections with the database. Similarly, DROP DATABASE FORCE will do the same. In the first terminal, create a test database and a database test, and connect to the database.

What is purge in PostgreSQL?

Using memory purge configuration parameters, Postgres Pro Standard can automatically replace data with zero bytes before it is deleted. This section describes how different types of data are handled. By default, all the memory purge parameters are switched on.

Can I delete default postgres database?

Unix. The default directory for PostgreSQL is /usr/local/pgsql. To uninstall PostgreSQL, manually delete the PostgreSQL folder from the default directory. To delete the database files, manually delete the Data folder from the default directory.

How do I delete everything in PostgreSQL?

Use the TRUNCATE TABLE command.


2 Answers

psql -c "copy (select datname from pg_database where datname like '%june%') to stdout" | while read line; do
    echo "$line"
    #dropdb -i "$line"
done

Or using for loop which is more reliable (while block executed in the parent context so it is necessary to do some additional movements for its body):

for dbname in $(psql -c "copy (select datname from pg_database where datname like '%june%') to stdout") ; do
    echo "$dbname"
    #dropdb -i "$dbname"
done

Also option -X for psql (do not use ~/.psqlrc file) could be good to avoid unnecessary outputs.

For psql and dropdb utilities you probably need to provide the connection options (see documentation)

like image 68
Abelisto Avatar answered Oct 01 '22 13:10

Abelisto


Big thanks to Abelisto.

Here are our last script that works well, after many months of search.

#!/bin/bash
clear
export PGPASSWORD="xxxxx"

PATTERN=$1

echo "Pattern parameter: $PATTERN"

/usr/bin/psql -U odoo -d postgres -c "copy (select datname from pg_database where datname like '%$PATTERN%') to stdout" | while read line; do
    echo "$line"
    dropdb -U xxxx "$line"
done
echo
echo "Databases which names matches pattern $PATTERN were deleted!"
echo
exit
like image 30
La Page PT Avatar answered Oct 01 '22 14:10

La Page PT