Django拒绝删除PostgreSQL数据库[另一个使用该数据库的会话] [英] Django refuse to drop PostgreSQL database [another session using the database]

查看:119
本文介绍了Django拒绝删除PostgreSQL数据库[另一个使用该数据库的会话]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下设置为Django设置postgresql:

I try to setup postgresql for django, with the following setting:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'tangorblog_features',
        'TEST': {
            'NAME': 'tangorblog_features'
        }
    },
}

使用带有硒和萝卜bdd的开发服务器进行测试。我将运行开发服务器,并让selenium和Django LiveServerTestCase 针对该服务器进行测试,而无需创建单独的数据库。因此,每次测试运行时,都会重置数据库。但是Django拒绝使用数据库的其他会话。
但是,当我使用具有相同设置的mysql时:

The idea is to test using development server with selenium and radish-bdd. I will run the development server, and let selenium and Django LiveServerTestCase to test against that server, without creating a separate database. So each time the test run, the database is reset. But Django refuse that there are other session that using the database. However when I use mysql with the same settings like:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'tangorblog_features',
        'HOST': 'localhost',
        'PORT': '',
        'USER': 'goat',
        'PASSWORD': '',
        'TEST': {
            'NAME': 'tangorblog_features'
        }
    },
}

测试运行没有问题,大约在另一个会话中使用的数据库。我认为这是PostgreSQL问题。我该如何调整它,使其表现得像MySQL?

The test runs without a problem, about database being used in another session. I think that this is the PostgreSQL problem. How can I tweak it, so it could behave like MySQL?

推荐答案

这是因为mysql和postgres对数据库的处理不同。 Mysql数据库是postgres所谓的架构。 DROP SCHEMA 也很可能发生,就像 https://dev.mysql.com/doc/refman/5.7/en/drop-database.html DROP SCHEMA 在mysql中:

this happens because mysql and postgres treat database differently. Mysql database is what postgres calls schema. DROP SCHEMA would also happen quetly, just like https://dev.mysql.com/doc/refman/5.7/en/drop-database.html DROP SCHEMA in mysql:


DROP SCHEMA是DROP DATABASE的同义词。

DROP SCHEMA is a synonym for DROP DATABASE.

对于postgres,您连接到数据库以使用架构的方式也有所不同,因此,如果smbd想要删除数据库,则您已经终止了它们的会话。与在mysql中不同

Differently for postgres you connect to the database to work with schemas, so if smbd wants to drop database you have terminate their sessions. Not like in mysql


删除数据库不会删除该数据库中创建的
的任何TEMPORARY表。临时表在创建会话的会话结束时自动删除

Dropping a database does not remove any TEMPORARY tables that were created in that database. TEMPORARY tables are automatically removed when the session that created them ends.

这意味着会话不会在数据库删除时中止。

which means session is not aborted on database drop.

为了强制删除数据库和关于事实

In order to force database drop and regarding the fact


...在您或任何其他人连接到该文件时无法执行
目标数据库

...it cannot be executed while you or anyone else are connected to the target database

您必须先终止相应的后端,您可以使用

you have to terminate accordiing backends first, you can do it with

select pg_terminate_backend(pid)
from pg_stat_activity
where datname = 'tangorblog_features';

当然,您必须自己连接到其他数据库

Of course you have to be connected to some other db yourself

这篇关于Django拒绝删除PostgreSQL数据库[另一个使用该数据库的会话]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆