code: def parse_connection_string_psycopg2(connection_string):
      
      conn_prepared = {}
      conn_parsed = urlparse(connection_string)
      if not conn_parsed.hostname:
          _re_dbstr = re.compile(r'\bhost=(?P<host>[0-9a-zA-Z_.!@#$%^&*()~]+)|'
                                 r'dbname=(?P<dbname>[0-9a-zA-Z_.!@#$%^&*()~]+)|'
                                 r'port=(?P<port>[0-9a-zA-Z_.!@#$%^&*()~]+)|'
                                 r'user=(?P<user>[0-9a-zA-Z_.!@#$%^&*()~]+)|'
                                 r'password=(?P<password>[0-9a-zA-Z_.!@#$%^&*()~]+)\b', re.IGNORECASE)
          for match in _re_dbstr.finditer(connection_string):
              match_dict = match.groupdict()
              if match_dict['host']:
                  conn_prepared['host'] = match_dict['host']
              if match_dict['port']:
                  conn_prepared['port'] = match_dict['port']
              if match_dict['dbname']:
                  conn_prepared['dbname'] = match_dict['dbname']
              if match_dict['user']:
                  conn_prepared['user'] = match_dict['user']
              if match_dict['password']:
                  conn_prepared['password'] = match_dict['password']
      else:
          conn_prepared = {
              'host': conn_parsed.hostname,
              'port': conn_parsed.port,
              'dbname': conn_parsed.path,
              'user': conn_parsed.username,
              'password': conn_parsed.password
          }
  
      return conn_prepared
docs: parses psycopg2 consumable connection string
      :param connection_string:
      :return: return dictionary with connection string parts