Skip to content

Instantly share code, notes, and snippets.

@hfs
Created June 9, 2017 08:26
Show Gist options
  • Save hfs/db65ff72062e388e93f64aa3e76e422a to your computer and use it in GitHub Desktop.
Save hfs/db65ff72062e388e93f64aa3e76e422a to your computer and use it in GitHub Desktop.
PostgreSQL: List databases with no current activity
-- Show databases with no current connections
--
-- BIG FAT WARNING: Don't trust this blindly! Just because there's no *current*
-- activity does not mean a database is no longer needed.
--
SELECT
(pg_stat_file ('base/' || d.oid || '/PG_VERSION')).modification,
d.datname,
pg_size_pretty(pg_database_size(d.datname)) AS size
FROM pg_database d
LEFT JOIN pg_stat_activity a ON d.oid = a.datid
WHERE
a.query IS NULL AND
d.datname NOT LIKE 'template%'
ORDER BY datname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment