0

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'
        }
    },
}

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'
        }
    },
}

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?

tangorboyz
  • 39
  • 8
  • usually you see `another session using the database` when you try to drop it, not connect... – Vao Tsun Nov 02 '17 at 06:58
  • @Wyatt, that's the what I want, because I if I set different name for test, then selenium would test a different database. – tangorboyz Nov 02 '17 at 07:14
  • @VaoTsun you are right, my bad. – tangorboyz Nov 02 '17 at 07:15
  • Because I test using radish-bdd, the database will reset on each scenario, so I have to set it manually using test runner `DiscoverRunner`, between `@before.each_scenario` and `@after.each_scenario` – tangorboyz Nov 02 '17 at 07:31
  • With different name for test, for example, if I spin up the development server, then development server will use non-test database, while radish will reset the test database, which not what I expect. I want it to reset db on running server. – tangorboyz Nov 02 '17 at 07:35

1 Answers1

0

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 is a synonym for DROP DATABASE.

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

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

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
  • Ok, I think I need to stick with MySQL, unitl I figured out how to deal with it. The problem is need to automated the test. If I terminate the backend, then the django development server will fail. – tangorboyz Nov 02 '17 at 15:55