code: def detect_version():
      
      version_regex = re.compile(r'\(PostgreSQL\) (?P<major>\d)\.(?P<minor>\d)\.(?P<bugfix>\d)')
      pg_version = None
      with hide('running', 'stdout', 'stderr'):
          output = run('psql --version')
      match = version_regex.search(output)
      if match:
          result = match.groupdict()
          if 'major' in result and 'minor' in result:
              pg_version = u'%(major)s.%(minor)s' % result
      if not pg_version:
          abort(u"Error: Could not determine Postgres version of the server.")
      return pg_version
docs: Parse the output of psql to detect Postgres version.