Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hongphuc5497/d3c32c63be51da44536cffc46b55b988 to your computer and use it in GitHub Desktop.
Save hongphuc5497/d3c32c63be51da44536cffc46b55b988 to your computer and use it in GitHub Desktop.
How to drop multiple databases in PostgreSQL
  1. Connect to PostgreSQL server via command line: psql -d postgre
  2. At the prompt, type SQL query to construct DROP DATABASE statement then that statement via \gexec meta-command
  3. Replace test with your desired pattern

Some alternative ways :

  • Anonymous code block
  • Using a shell script
SELECT 'DROP DATABASE ' || quote_ident(datname) || ';'
FROM pg_database
WHERE datname LIKE 'test%' AND datistemplate=false

\gexec
@ismael3s
Copy link

ismael3s commented Dec 8, 2023

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment