0👍
My answer was to create a script like this:
export PGPASSWORD=the_password
if [[ `psql -h 127.0.0.1 -d postgres -U username -p 5432 -tAc "SELECT 1 FROM pg_database WHERE datname='test_djangoprojectname'"` == "1" ]]
then
psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'test_djangoprojectname' AND pid <> pg_backend_pid();"
psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "DROP DATABASE test_djangoprojectname;"
fi
- The
-d
setting is database name – it can be any database your user has access to, except the one you are deleting. - The default username is
postgres
. - The
-p
setting is the port your database is on –5432
is the default.
Save as (for example) del_test_db.sh
(Windows users see below), then
chmod +x del_test_db.sh
Then in PyCharm:
- Run, Edit Configurations…
- Unfold Defaults, click Django tests
- In the Before launch window click +, External tools, click +
- Under Program, select your file
del_test_db.sh
, give the command a name (eg, ‘del test db’) and click OK. - Select your tool in the list and click OK
- You may need to unfold Django tests in the left and delete existing test configurations
Then the script force deletes the test database before every run.
This works on Mac OS X and Ubuntu etc. For Windows the process is the same, except instead of export
use SET
, save the commands as a .bat
file instead of .sh
, and you don’t need to chmod +x
, and use the following syntax for the IF statement in the batch file:
if 'command' == '1' (
...
)
Apologies I’m unable to check this as I don’t have a Windows machine.
Thanks to this answer for the code checking whether the database exists.
22👍
- Django session expiry?
- How do I get django runserver to show me DeprecationWarnings and other useful messages?
- UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
- Django ALLOWED_HOSTS with ELB HealthCheck
6👍
If you’re using PyCharm and want to run a single test using the green arrow but you keep getting this error, you can modify the default django tests configuration template, so that you don’t have to keep setting the --noinput
option on each.
- Use a django built in filter in code (outside of a template)
- Django: Faking a field in the admin interface?
3👍