#29613 closed Bug (fixed)
Allow --keepdb to work on PostgreSQL if the database exists but the user can't create databases
Reported by: | Paul McDermott | Owned by: | Mariusz Felisiak |
---|---|---|---|
Component: | Testing framework | Version: | 2.0 |
Severity: | Normal | Keywords: | postgres, --keepdb |
Cc: | Mariusz Felisiak | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The popular Web Faction hosting service uses a shared database server. Users can create databases using the web UI or XML RPC calls, but not using the SQL CREATE syntax.
Running tests throws a ProgrammingError, with the message 'permission denied to create database', even if the test database has been previously created manually.
The error code for this error is '42501', which appears to correspond to errorcodes.INSUFFICIENT_PRIVILEGE.
django/db/backends/postgresql/creation.py only handles the error errorcodes.DUPLICATE_DATABASE in _execute_create_test_db(), line 35. Because the error code does not match the program exits with a log message. But it would be fine to proceed with the error code '42501' also, making use of the --keepdb mechanism.
This appears to be a regression, as I did not experience this issue either using postgresql_psycopg2 driver or using Django 1.11
Change History (11)
comment:1 by , 6 years ago
follow-up: 5 comment:2 by , 6 years ago
The change in behavior is due to e776dd2db677d58dcb50aea20d3bb191537df25b (#28116). Isn't the problem with your proposal that it would hide cases where the database doesn't exist and the permissions are insufficient to create it?
comment:3 by , 6 years ago
Cc: | added |
---|
Agreed with TIm, if you created database in advance, 42P04 - Duplicate database
should be raised. Your proposal will hide useful information for all users without sufficient privileges.
comment:4 by , 6 years ago
I can understand that PostgreSQL will error with "insufficient permissions". We could to try to emulate CREATE DATABASE IF NOT EXISTS.
comment:5 by , 6 years ago
Replying to Tim Graham:
Isn't the problem with your proposal that it would hide cases where the database doesn't exist and the permissions are insufficient to create it?
That may be the case, but a little bit of manual tweaking is implicit in the use of --keepdb for this use case (that is deploying to a shared database environment where one cannot create databases as a user).
I understand that there are other uses for the --keepdb flag to speed up testing. Maybe there should really be 2 flags with different logic:
--dont_try_creating_database_because_it_wont_work; and
--keep_the_database_around_cos_I_like_the_speed
(exaggerating for effect).
comment:6 by , 6 years ago
Component: | Database layer (models, ORM) → Testing framework |
---|---|
Summary: | --keepdb flag for postgresql does not work with some database error codes → Allow --keepdb to work on PostgreSQL if the database exists but the user can't create databases |
Triage Stage: | Unreviewed → Accepted |
I don't think adding another flag is a nice option.
comment:7 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:9 by , 6 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
The following modification to _execute_create_test_db() got the --keepdb flag working again for me on Web Faction