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

前端 未结 16 2083
时光取名叫无心
时光取名叫无心 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:01

    After restarting the server or computer, please try again.

    It could be the simple solution.

    0 讨论(0)
  • 2020-12-04 07:02

    Please check if your rails console or server is running in another tab and then

    stop the rails server and console.

    then run

     rake db:drop
    
    0 讨论(0)
  • 2020-12-04 07:06

    When we used the "kill processes" method from above, the db:drop was failing (if :kill_postgres_connections was prerequisite). I believe it was because the connection which that rake command was using was being killed. Instead, we are using a sql command to drop the connection. This works as a prerequisite for db:drop, avoids the risk of killing processes via a rather complex command, and it should work on any OS (gentoo required different syntax for kill).

    cmd = %(psql -c "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE procpid <> pg_backend_pid();" -d '#{db_name}')
    

    Here is a rake task that reads the database name from database.yml and runs an improved (IMHO) command. It also adds db:kill_postgres_connections as a prerequisite to db:drop. It includes a warning that yells after you upgrade rails, indicating that this patch may no longer be needed.

    see: https://gist.github.com/4455341, references included

    0 讨论(0)
  • 2020-12-04 07:06

    I had this same issue when working with a Rails 5.2 application and PostgreSQL database in production.

    Here's how I solved it:

    First, log out every connection to the database server on the PGAdmin Client if any.

    Stop every session using the database from the terminal.

    sudo kill -9 `ps -u postgres -o pid=`
    

    Start the PostgreSQL server, since the kill operation above stopped the PostgreSQL server.

    sudo systemctl start postgresql
    

    Drop the database in the production environment appending the production arguments.

    rails db:drop RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1
    

    That's all.

    I hope this helps

    0 讨论(0)
  • 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 = <<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.

    0 讨论(0)
  • 2020-12-04 07:10

    Easier and more updated way is: 1. Use ps -ef | grep postgres to find the connection # 2. sudo kill -9 "# of the connection

    Note: There may be identical PID. Killing one kills all.

    0 讨论(0)
提交回复
热议问题