Skip to content

Instantly share code, notes, and snippets.

@gmhawash
Last active October 19, 2018 04:31
Show Gist options
  • Save gmhawash/9145355 to your computer and use it in GitHub Desktop.
Save gmhawash/9145355 to your computer and use it in GitHub Desktop.
Rename a postgresql database with open connection
Ref: http://stackoverflow.com/questions/5108876/kill-a-postgresql-session-connection
Postgres (rightfully) stops you from renaming a database while it has connections open connections. To test an exception handler which attempts to handle lost database connection, I was able to do the following:
1. \c (database that you are not trying to rename)
\c template1
2. select pg_terminate_backend(pid) from pg_stat_activity where datname ~ 'db_name_you_are_trying_to_rename';
3. alter database db_name_you_are_trying_to_rename rename to new_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment