回答(15)
2 years ago
您可以使用pg_terminate_backend()来终止连接 . 您必须是超级用户才能使用此功能 . 这适用于所有操作系统 .
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'
;
在执行此查询之前,您必须REVOKE CONNECT权限以避免新连接:
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;
如果您正在使用Postgres 8.4-9.1,请使用procpid而不是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'
;
2 years ago
也许只需重启 postgres => sudo service postgresql restart
2 years ago
有关运行过程的所有信息:
SELECT *, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
AND datname = 'my_database_name';
2 years ago
OSX,Postgres 9.2(随自制软件一起安装)
$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ pg_ctl restart -D /usr/local/var/postgres
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
如果你的datadir在别处你可以通过检查 ps aux | grep postgres 的输出找出它的位置
2 years ago
这似乎适用于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'] =~ //
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
end
end
ActiveRecord::Base.connection.drop_database config['database']
end
end
2 years ago
MacOS,如果postgresql是用brew安装的:
brew services restart postgresql
2 years ago
我使用以下rake任务来覆盖Rails drop_database 方法 .
lib/database.rake
require 'active_record/connection_adapters/postgresql_adapter'
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
def drop_database(name)
raise "Nah, I won't drop the production database" if Rails.env.production?
execute <
UPDATE pg_catalog.pg_database
SET datallowconn=false WHERE datname='#{name}'
SQL
execute <
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '#{name}';
SQL
execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
end
end
end
end
编辑:这是Postgresql 9.2
2 years ago
我有这个问题,问题是Navicat连接到我当地的Postgres数据库 . 一旦我断开Navicat,问题就消失了 .
编辑:
sudo kill -15 `ps -u postgres -o pid`
...将杀死postgres用户正在访问的所有内容 . 避免在 生产环境 计算机上执行此操作,但您不应该遇到开发环境问题 . 在此之后尝试重新启动PostgreSQL之前,确保每个 postgres 进程真正终止是至关重要的 .
编辑2:
由于this unix.SE post我已从 kill -9 更改为 kill -15 .
2 years ago
退出postgres并重新启动它 . 很简单,但每次都适合我,其他cli命令有时不工作 .
2 years ago
我已经解决了这个问题:
在我的 Windows8 64 位,只需 restart 服务: postgresql-x64-9.5
2 years ago
只是想指出,如果其他一些后台进程正在使用数据库,Haris的答案可能无效,在我的情况下,它是延迟的工作,我做了:
script/delayed_job stop
只有这样我才能删除/重置数据库 .
2 years ago
没有必要放弃它 . 只需删除并重新创建公共模式即可 . 在大多数情况下,这具有完全相同的效果 .
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
2 years ago
远程方案 . 但是如果你试图在rails应用程序中运行测试,那么你会得到类似的东西
“ActiveRecord :: StatementInvalid:PG :: ObjectInUse:错误:其他用户正在访问数据库”myapp_test“细节:还有一个使用数据库的会话 . ”
确保在运行测试之前关闭pgAdmin或任何其他postgres GUI工具 .
2 years ago
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid()
-- no need to kill connections to other databases
AND datname = current_database();
-- use current_database by opening right query tool
2 years ago
案件 :
无法执行查询:
DROP TABLE dbo.t_tabelname
方案:
一个 . 显示查询状态活动如下:
SELECT * FROM pg_stat_activity ;
湾查找“查询”列包含的行:
'DROP TABLE dbo.t_tabelname'
C . 在同一行中,获取'PID'列的值
example : 16409
d . 执行这些脚本:
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'
;