Rails + Postgres drop error: database is being accessed by other users

前端 未结 16 2116
时光取名叫无心
时光取名叫无心 2020-12-04 06:26

I have a rails application running over Postgres.

I have two servers: one for testing and the other for production.

Very often I need to clone the production

16条回答
  •  自闭症患者
    2020-12-04 07:07

    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 = < :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.

提交回复
热议问题