且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails + Postgres放置错误:其他用户正在访问数据库

更新时间:2023-12-02 13:29:34

如果杀死正在运行的postgresql连接到您的应用程序,然后就可以运行db:drop了。那么如何杀死那些联系呢?我使用以下rake任务:

If you kill the running postgresql connections for your application, you can then run db:drop just fine. So how to kill those connections? I use the following rake task:

# lib/tasks/kill_postgres_connections.rake
task :kill_postgres_connections => :environment do
  db_name = "#{File.basename(Rails.root)}_#{Rails.env}"
  sh = <<EOF
ps xa \
  | grep postgres: \
  | grep #{db_name} \
  | grep -v grep \
  | awk '{print $1}' \
  | xargs kill
EOF
  puts `#{sh}`
end

task "db:drop" => :kill_postgres_connections

从导轨下方拔出连接有时会在下次尝试时将其阻塞加载页面,但再次重新加载会重新建立连接。

Killing the connections out from under rails will sometimes cause it to barf the next time you try to load a page, but reloading it again re-establishes the connection.