每次我尝试从界面复制或删除数据库时,我总是会收到此错误,即使所有用户都已注销:
ERROR: source database "database_name" is being accessed by other users DETAIL: There are 5 other sessions using the database.
然后,如果想要复制或删除数据库,我必须运行此查询以终止所有连接:
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'database_name' AND pid <> pg_backend_pid();
我想要做的是覆盖以下控制器,以便在执行函数duplicate_database和drop之前始终运行查询:
@http.route('/web/database/duplicate',type='json',auth="none") def duplicate(self,fields): params = dict(map(operator.itemgetter('name','value'),fields)) duplicate_attrs = ( params['super_admin_pwd'],params['db_original_name'],params['db_name'],) return request.session.proxy("db").duplicate_database(*duplicate_attrs) @http.route('/web/database/drop',auth="none") def drop(self,fields): password,db = operator.itemgetter( 'drop_pwd','drop_db')( dict(map(operator.itemgetter('name',fields))) try: if request.session.proxy("db").drop(password,db): return True else: return False except openerp.exceptions.AccessDenied: return {'error': 'AccessDenied','title': 'Drop Database'} except Exception: return {'error': _('Could not drop database !'),'title': _('Drop Database')}
我在Documentation of Odoo中找到了这个注释:
[…] This operation requires that there be no connection to the
database being duplicated,but Odoo doesn’t currently break
existing/outstanding connections,so restarting the server is the
simplest way to ensure everything is in the right state.
可以安全地覆盖这些控制器吗?有什么更好的方法呢?我想使用界面执行此操作,因为如果我手动执行此操作或者每次需要我必须停止服务器都非常烦人.
在客户端连接到postgres数据库时,您无法删除它.相当强大的解决方法,是
原文链接:https://www.f2er.com/postgresql/192115.html确保没有人可以连接到此数据库
update pg_database set datallowconn = 'false' where datname = 'mydb';
强制断开连接到此数据库的所有客户端.
对于postgres< 9.2:
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = 'mydb';
对于postgres版本> = 9.2将procpid更改为pid:
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb';
算了吧
DROP DATABASE mydb;
你不能只使用dropdb实用程序 – 这是DROP DATABASE服务器查询的简单包装器.