Fixed #32456 -- Added dbshell support for specifying a password file on PostgreSQL.

This commit is contained in:
Hasan Ramezani 2021-02-24 17:16:45 +01:00 committed by Mariusz Felisiak
parent 9f125fce79
commit 8380fe08a0
4 changed files with 48 additions and 6 deletions

View File

@ -16,6 +16,7 @@ class DatabaseClient(BaseDatabaseClient):
dbname = settings_dict.get('NAME') dbname = settings_dict.get('NAME')
user = settings_dict.get('USER') user = settings_dict.get('USER')
passwd = settings_dict.get('PASSWORD') passwd = settings_dict.get('PASSWORD')
passfile = options.get('passfile')
service = options.get('service') service = options.get('service')
sslmode = options.get('sslmode') sslmode = options.get('sslmode')
sslrootcert = options.get('sslrootcert') sslrootcert = options.get('sslrootcert')
@ -48,6 +49,8 @@ class DatabaseClient(BaseDatabaseClient):
env['PGSSLCERT'] = str(sslcert) env['PGSSLCERT'] = str(sslcert)
if sslkey: if sslkey:
env['PGSSLKEY'] = str(sslkey) env['PGSSLKEY'] = str(sslkey)
if passfile:
env['PGPASSFILE'] = str(passfile)
return args, env return args, env
def runshell(self, parameters): def runshell(self, parameters):

View File

@ -115,9 +115,9 @@ PostgreSQL connection settings
See :setting:`HOST` for details. See :setting:`HOST` for details.
To connect using a service name from the `connection service file`_, you must To connect using a service name from the `connection service file`_ and a
specify it in the :setting:`OPTIONS` part of your database configuration in password from the `password file`_, you must specify them in the
:setting:`DATABASES`: :setting:`OPTIONS` part of your database configuration in :setting:`DATABASES`:
.. code-block:: python .. code-block:: python
:caption: settings.py :caption: settings.py
@ -125,7 +125,10 @@ specify it in the :setting:`OPTIONS` part of your database configuration in
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql', 'ENGINE': 'django.db.backends.postgresql',
'OPTIONS': {'service': 'my_service'}, 'OPTIONS': {
'service': 'my_service',
'passfile': '.my_pgpass',
},
} }
} }
@ -136,14 +139,20 @@ specify it in the :setting:`OPTIONS` part of your database configuration in
host=localhost host=localhost
user=USER user=USER
dbname=NAME dbname=NAME
password=PASSWORD
port=5432 port=5432
.. code-block:: text
:caption: .my_pgpass
localhost:5432:NAME:USER:PASSWORD
.. _connection service file: https://www.postgresql.org/docs/current/libpq-pgservice.html .. _connection service file: https://www.postgresql.org/docs/current/libpq-pgservice.html
.. _password file: https://www.postgresql.org/docs/current/libpq-pgpass.html
.. versionchanged:: 4.0 .. versionchanged:: 4.0
Support for connecting by a service name was added. Support for connecting by a service name, and specifying a password file
was added.
Optimizing PostgreSQL's configuration Optimizing PostgreSQL's configuration
------------------------------------- -------------------------------------

View File

@ -206,6 +206,8 @@ Management Commands
* The :djadmin:`runserver` management command now supports the * The :djadmin:`runserver` management command now supports the
:option:`--skip-checks` option. :option:`--skip-checks` option.
* On PostgreSQL, :djadmin:`dbshell` now supports specifying a password file.
Migrations Migrations
~~~~~~~~~~ ~~~~~~~~~~

View File

@ -73,6 +73,34 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
(['psql'], {'PGSERVICE': 'django_test'}), (['psql'], {'PGSERVICE': 'django_test'}),
) )
def test_passfile(self):
self.assertEqual(
self.settings_to_cmd_args_env({
'NAME': 'dbname',
'USER': 'someuser',
'HOST': 'somehost',
'PORT': '444',
'OPTIONS': {
'passfile': '~/.custompgpass',
},
}),
(
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
{'PGPASSFILE': '~/.custompgpass'},
),
)
self.assertEqual(
self.settings_to_cmd_args_env({
'OPTIONS': {
'service': 'django_test',
'passfile': '~/.custompgpass',
},
}),
(
['psql'], {'PGSERVICE': 'django_test', 'PGPASSFILE': '~/.custompgpass'},
),
)
def test_column(self): def test_column(self):
self.assertEqual( self.assertEqual(
self.settings_to_cmd_args_env({ self.settings_to_cmd_args_env({