Created
March 8, 2019 17:09
-
-
Save vadviktor/e3fc81a5c1a286a02652952a6b8220ab to your computer and use it in GitHub Desktop.
Rails: kill all PostgreSQL connections.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace :db do | |
| desc "Kick out PostgreSQL users from the database." | |
| task kick: [:environment] do | |
| begin | |
| ActiveRecord::Base.connection.execute <<-SQL | |
| SELECT pg_terminate_backend(pg_stat_activity.pid) | |
| FROM pg_stat_activity | |
| WHERE pg_stat_activity.datname = '#{ActiveRecord::Base.connection.current_database}'; | |
| SQL | |
| rescue ActiveRecord::StatementInvalid | |
| puts "All connections killed." | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment