summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/client.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2020-10-04 18:25:29 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-29 22:22:58 +0100
commitbbe6fbb8768e8fb1aecb96d51c049d7ceaf802d3 (patch)
tree7c0e7ac6defc405cd5320066f75ade21bf9943ac /django/db/backends/postgresql/client.py
parent4ac2d4fa42e1659f328c35b6b8d4761b3419c11a (diff)
downloaddjango-bbe6fbb8768e8fb1aecb96d51c049d7ceaf802d3.tar.gz
Refs #32061 -- Unified DatabaseClient.runshell() in db backends.
Diffstat (limited to 'django/db/backends/postgresql/client.py')
-rw-r--r--django/db/backends/postgresql/client.py47
1 files changed, 23 insertions, 24 deletions
diff --git a/django/db/backends/postgresql/client.py b/django/db/backends/postgresql/client.py
index 9d390b3807..7965401163 100644
--- a/django/db/backends/postgresql/client.py
+++ b/django/db/backends/postgresql/client.py
@@ -1,6 +1,4 @@
-import os
import signal
-import subprocess
from django.db.backends.base.client import BaseDatabaseClient
@@ -9,18 +7,19 @@ class DatabaseClient(BaseDatabaseClient):
executable_name = 'psql'
@classmethod
- def runshell_db(cls, conn_params, parameters):
+ def settings_to_cmd_args_env(cls, settings_dict, parameters):
args = [cls.executable_name]
-
- host = conn_params.get('host', '')
- port = conn_params.get('port', '')
- dbname = conn_params.get('database', '')
- user = conn_params.get('user', '')
- passwd = conn_params.get('password', '')
- sslmode = conn_params.get('sslmode', '')
- sslrootcert = conn_params.get('sslrootcert', '')
- sslcert = conn_params.get('sslcert', '')
- sslkey = conn_params.get('sslkey', '')
+ options = settings_dict.get('OPTIONS', {})
+
+ host = settings_dict.get('HOST')
+ port = settings_dict.get('PORT')
+ dbname = settings_dict.get('NAME') or 'postgres'
+ user = settings_dict.get('USER')
+ passwd = settings_dict.get('PASSWORD')
+ sslmode = options.get('sslmode')
+ sslrootcert = options.get('sslrootcert')
+ sslcert = options.get('sslcert')
+ sslkey = options.get('sslkey')
if user:
args += ['-U', user]
@@ -31,25 +30,25 @@ class DatabaseClient(BaseDatabaseClient):
args += [dbname]
args.extend(parameters)
- sigint_handler = signal.getsignal(signal.SIGINT)
- subprocess_env = os.environ.copy()
+ env = {}
if passwd:
- subprocess_env['PGPASSWORD'] = str(passwd)
+ env['PGPASSWORD'] = str(passwd)
if sslmode:
- subprocess_env['PGSSLMODE'] = str(sslmode)
+ env['PGSSLMODE'] = str(sslmode)
if sslrootcert:
- subprocess_env['PGSSLROOTCERT'] = str(sslrootcert)
+ env['PGSSLROOTCERT'] = str(sslrootcert)
if sslcert:
- subprocess_env['PGSSLCERT'] = str(sslcert)
+ env['PGSSLCERT'] = str(sslcert)
if sslkey:
- subprocess_env['PGSSLKEY'] = str(sslkey)
+ env['PGSSLKEY'] = str(sslkey)
+ return args, env
+
+ def runshell(self, parameters):
+ sigint_handler = signal.getsignal(signal.SIGINT)
try:
# Allow SIGINT to pass to psql to abort queries.
signal.signal(signal.SIGINT, signal.SIG_IGN)
- subprocess.run(args, check=True, env=subprocess_env)
+ super().runshell(parameters)
finally:
# Restore the original SIGINT handler.
signal.signal(signal.SIGINT, sigint_handler)
-
- def runshell(self, parameters):
- self.runshell_db(self.connection.get_connection_params(), parameters)