Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Created May 28, 2019 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlexArcPy/f62de066f607acb6699e37a8e14cef1c to your computer and use it in GitHub Desktop.
Save AlexArcPy/f62de066f607acb6699e37a8e14cef1c to your computer and use it in GitHub Desktop.
Drop PostgreSQL database matching name pattern
echo "Drop PostgreSQL database matching a pattern"
cd C:/Temp
sql_query="select 'drop database \"'||datname||'\";' from pg_database WHERE datistemplate = false and datname like 'operat%';"
echo $sql_query
PGPASSWORD=postgres psql -p 5432 -d postgres -U postgres -t -A -F"," -c "${sql_query}" > drop_dbs.sql
cat drop_dbs.sql | while read line
do
echo $line;
PGPASSWORD=postgres psql -p 5432 -d postgres -U postgres -t -A -F"," -c "${line}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment