Skip to content

Instantly share code, notes, and snippets.

@kenaniah
Created July 28, 2015 21:15
Show Gist options
  • Save kenaniah/88f0f8b12cf21d383928 to your computer and use it in GitHub Desktop.
Save kenaniah/88f0f8b12cf21d383928 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This file is being maintained by Puppet.
# DO NOT EDIT
# Usage information
if [ $# -ne 2 ]
then
echo "Usage: $0 <original_name> <new_name>"
echo
echo "This script will rename a database after disconnecting clients."
exit 1
fi
# Close all connections to the requested database
echo "Closing connections to database $1..."
psql -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='$1'"
# Rename the database
echo "Renaming database $1 to $2..."
psql -c "ALTER DATABASE \"$1\" RENAME TO \"$2\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment