How to delete postgresql databases matching a pattern?
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)