Kill a postgresql session/connection

前端 未结 20 1831
暖寄归人
暖寄归人 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:33

    Open PGadmin see if there is any query page open, close all query page and disconnect the PostgresSQL server and Connect it again and try delete/drop option.This helped me.

    0 讨论(0)
  • 2020-11-28 00:35

    I'm on a mac and I use postgres via Postgres.app. I solved this problem just quitting and starting again the app.

    0 讨论(0)
  • 2020-11-28 00:37

    Maybe just restart postgres => sudo service postgresql restart

    0 讨论(0)
  • 2020-11-28 00:39

    This seems to be working for PostgreSQL 9.1:

    #{Rails.root}/lib/tasks/databases.rake
    # monkey patch ActiveRecord to avoid There are n other session(s) using the database.
    def drop_database(config)
      case config['adapter']
      when /mysql/
        ActiveRecord::Base.establish_connection(config)
        ActiveRecord::Base.connection.drop_database config['database']
      when /sqlite/
        require 'pathname'
        path = Pathname.new(config['database'])
        file = path.absolute? ? path.to_s : File.join(Rails.root, path)
    
        FileUtils.rm(file)
      when /postgresql/
        ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
        ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
          if config['database'] == x['datname'] && x['current_query'] =~ /<IDLE>/
            ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
          end
        end
        ActiveRecord::Base.connection.drop_database config['database']
      end
    end
    

    Lifted from gists found here and here.

    Here's a modified version that works for both PostgreSQL 9.1 and 9.2.

    0 讨论(0)
  • 2020-11-28 00:41

    Remote scenario. But if you're trying to run tests in a rails app, and you get something like

    "ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: database "myapp_test" is being accessed by other users DETAIL: There is 1 other session using the database."

    Make sure you close pgAdmin or any other postgres GUI tools before running tests.

    0 讨论(0)
  • 2020-11-28 00:42

    Case :
    Fail to execute the query :

    DROP TABLE dbo.t_tabelname
    

    Solution :
    a. Display query Status Activity as follow :

    SELECT * FROM pg_stat_activity  ;
    

    b. Find row where 'Query' column has contains :

    'DROP TABLE dbo.t_tabelname'
    

    c. In the same row, get value of 'PID' Column

    example : 16409
    

    d. Execute these scripts :

    SELECT 
        pg_terminate_backend(25263) 
    FROM 
        pg_stat_activity 
    WHERE 
        -- don't kill my own connection!
        25263 <> pg_backend_pid()
        -- don't kill the connections to other databases
        AND datname = 'database_name'
        ;
    
    0 讨论(0)
提交回复
热议问题