Kill a postgresql session/connection

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

    You can use pg_terminate_backend() to kill a connection. You have to be superuser to use this function. This works on all operating systems the same.

    SELECT 
        pg_terminate_backend(pid) 
    FROM 
        pg_stat_activity 
    WHERE 
        -- don't kill my own connection!
        pid <> pg_backend_pid()
        -- don't kill the connections to other databases
        AND datname = 'database_name'
        ;
    

    Before executing this query, you have to REVOKE the CONNECT privileges to avoid new connections:

    REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;
    

    If you're using Postgres 8.4-9.1 use procpid instead of pid

    SELECT 
        pg_terminate_backend(procpid) 
    FROM 
        pg_stat_activity 
    WHERE 
        -- don't kill my own connection!
        procpid <> pg_backend_pid()
        -- don't kill the connections to other databases
        AND datname = 'database_name'
        ;
    
    0 讨论(0)
  • 2020-11-28 00:28

    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)
  • 2020-11-28 00:28

    For me worked the following:

    sudo gitlab-ctl stop
    sudo gitlab-ctl start gitaly
    sudo gitlab-rake gitlab:setup [type yes and let it finish]
    sudo gitlab-ctl start
    

    I am using:
    gitlab_edition: "gitlab-ce"
    gitlab_version: '12.4.0-ce.0.el7'

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

    In PG admin you can disconnect your server (right click on the server) & all sessions will be disconnected at restart

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-28 00:33

    Just wanted to point out that Haris's Answer might not work if some other background process is using the database, in my case it was delayed jobs, I did:

    script/delayed_job stop
    

    And only then I was able to drop/reset the database.

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