Kill a postgresql session/connection

前端 未结 20 1923
暖寄归人
暖寄归人 2020-11-28 00:11

How can I kill all my postgresql connections?

I\'m trying a rake db:drop but I get:

ERROR:  database \"database_name\" is being accessed         


        
20条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 00:32

    There is no need to drop it. Just delete and recreate the public schema. In most cases this have exactly the same effect.

    namespace :db do
    
    desc 'Clear the database'
    task :clear_db => :environment do |t,args|
      ActiveRecord::Base.establish_connection
      ActiveRecord::Base.connection.tables.each do |table|
        next if table == 'schema_migrations'
        ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
      end
    end
    
    desc 'Delete all tables (but not the database)'
    task :drop_schema => :environment do |t,args|
      ActiveRecord::Base.establish_connection
      ActiveRecord::Base.connection.execute("DROP SCHEMA public CASCADE")
      ActiveRecord::Base.connection.execute("CREATE SCHEMA public")
      ActiveRecord::Base.connection.execute("GRANT ALL ON SCHEMA public TO postgres")
      ActiveRecord::Base.connection.execute("GRANT ALL ON SCHEMA public TO public")
      ActiveRecord::Base.connection.execute("COMMENT ON SCHEMA public IS 'standard public schema'")
    end
    
    desc 'Recreate the database and seed'
    task :redo_db => :environment do |t,args|
      # Executes the dependencies, but only once
      Rake::Task["db:drop_schema"].invoke
      Rake::Task["db:migrate"].invoke
      Rake::Task["db:migrate:status"].invoke 
      Rake::Task["db:structure:dump"].invoke
      Rake::Task["db:seed"].invoke
    end
    
    end
    

提交回复
热议问题