how to drop all databases except few ones in postgres

First, execute the following query in the psql terminal.

select 'drop database "'||datname||'";'
from pg_database
where datistemplate=false;

This will generate drop database command for all the databases. Copy the result in a text editor and exclude(delete) what you want to keep and save it as dd.sql file. And execute it like this:

psql -d postgres -f dd.sql

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)