Opened 8 months ago

Closed 8 months ago

Last modified 8 months ago

#35394 closed Cleanup/optimization (needsinfo)

Warning if `postgres` database has been renamed (e.g. DigitalOcean managed)

Reported by: Jeffry Babb Owned by: nobody
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords: postgres default db name
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Natalia Bidart)

This warning is thrown if the postgres database does not exist on the server. DigitalOcean renames this database to 'defaultdb' which seems to raise this error in dango/db/backends/postgresql/base.py

        except (Database.DatabaseError, WrappedDatabaseError):
            if cursor is not None:
                raise
            warnings.warn(
                "Normally Django will use a connection to the 'postgres' database "
                "to avoid running initialization queries against the production "
                "database when it's not needed (for example, when running tests). "
                "Django was unable to create a connection to the 'postgres' database "
                "and will use the first PostgreSQL database instead.",
                RuntimeWarning,
            )

I have searched in the LIBPQ parameters can can't find a setting that allows me to specify this name change as Django seems to have hard-coded the search for the postgres db name or is looking for it in a key called 'service' around line 193 and 218 in base.py.

Change History (3)

comment:1 by Natalia Bidart, 8 months ago

Resolution: needsinfo
Status: newclosed

Hello Jeffry, thank you for taking the time to submit this report.

We would need more information to fully triage the issue, as I only see the postgres DB name hardcoded in two places:

  1. get_connection_params in django/db/backends/postgresql/base.py, specifically when NAME is None.
  2. settings_to_cmd_args_env in django/db/backends/postgresql/client.py when the dbname is unset.

A few questions:

  1. Could you please add specific details on when and where are you getting the warning? The warning text mentions that the postgres DB may be used in context such as things like running tests. Are you running the test suite in Heroku for example?
  2. Is there a specific issue that this warning triggers for you? If everything is working well, you could just ignore the specific warning with filters.
  3. Can you provide detailed instructions for reproducing locally/outside Heroku? Have you double check that the DB name is set properly?
  4. Did you try seeking initial help in the Django Forum, to double check whether this is truly an issue in Django and not in your setup?

I'll be closing this ticket as needsinfo following the ticket triaging process.

comment:2 by Natalia Bidart, 8 months ago

Description: modified (diff)
Summary: Warning if 'postgres' database has been rename (e.g. DigitalOcean managed)Warning if `postgres` database has been rename (e.g. DigitalOcean managed)

comment:3 by Tim Graham, 8 months ago

Summary: Warning if `postgres` database has been rename (e.g. DigitalOcean managed)Warning if `postgres` database has been renamed (e.g. DigitalOcean managed)

The postgres database being named differently on Heroku was the impetus for adding the fallback and the warning (#24791). Making the name of the defaultdb configurable was proposed as a possible solution but there weren't any arguments for or against. The fallback solution makes things easy for the user, although the warning is mildly annoying and hardcoding 'postgres' leaves no way to configure things safely. The docstring ofBaseDatabaseWrapper._nodb_cursor() explains possible dangers of the current fallback approach.

I can't think of a better way to make this configurable than to add a new option in DATABASES like DEFAULT_DB, although it appears only applicable on PostgreSQL. I'm not sure if it's worth it. I would continue the discussion on the mailing list.

Note: See TracTickets for help on using tickets.
Back to Top