diff options
author | Caio Ariede <caio.ariede@gmail.com> | 2015-08-05 11:08:56 -0300 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2015-08-07 09:33:17 -0400 |
commit | ec9004728ee136e3b7e2b7cd2610203e16b6ce9b (patch) | |
tree | bd3fd3d8c729e5e01fc3111696a7d4eaa56d85b0 /django/db/backends/postgresql/client.py | |
parent | 8656cfc4e01332426e5e4b78c20a4e9ec443b293 (diff) | |
download | django-ec9004728ee136e3b7e2b7cd2610203e16b6ce9b.tar.gz |
Fixed #25175 -- Renamed the postgresql_psycopg2 database backend to postgresql.
Diffstat (limited to 'django/db/backends/postgresql/client.py')
-rw-r--r-- | django/db/backends/postgresql/client.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/django/db/backends/postgresql/client.py b/django/db/backends/postgresql/client.py new file mode 100644 index 0000000000..5e3e288301 --- /dev/null +++ b/django/db/backends/postgresql/client.py @@ -0,0 +1,66 @@ +import os +import subprocess + +from django.core.files.temp import NamedTemporaryFile +from django.db.backends.base.client import BaseDatabaseClient +from django.utils.six import print_ + + +def _escape_pgpass(txt): + """ + Escape a fragment of a PostgreSQL .pgpass file. + """ + return txt.replace('\\', '\\\\').replace(':', '\\:') + + +class DatabaseClient(BaseDatabaseClient): + executable_name = 'psql' + + @classmethod + def runshell_db(cls, settings_dict): + args = [cls.executable_name] + + host = settings_dict.get('HOST', '') + port = settings_dict.get('PORT', '') + name = settings_dict.get('NAME', '') + user = settings_dict.get('USER', '') + passwd = settings_dict.get('PASSWORD', '') + + if user: + args += ['-U', user] + if host: + args += ['-h', host] + if port: + args += ['-p', str(port)] + args += [name] + + temp_pgpass = None + try: + if passwd: + # Create temporary .pgpass file. + temp_pgpass = NamedTemporaryFile(mode='w+') + try: + print_( + _escape_pgpass(host) or '*', + str(port) or '*', + _escape_pgpass(name) or '*', + _escape_pgpass(user) or '*', + _escape_pgpass(passwd), + file=temp_pgpass, + sep=':', + flush=True, + ) + os.environ['PGPASSFILE'] = temp_pgpass.name + except UnicodeEncodeError: + # If the current locale can't encode the data, we let + # the user input the password manually. + pass + subprocess.call(args) + finally: + if temp_pgpass: + temp_pgpass.close() + if 'PGPASSFILE' in os.environ: # unit tests need cleanup + del os.environ['PGPASSFILE'] + + def runshell(self): + DatabaseClient.runshell_db(self.connection.settings_dict) |