code: def excute_query(query, db=None, flags=None, use_sudo=False, **kwargs):
      
  
      flags = flags or u''
      if db:
          flags = u"%s -d %s" % (flags, db)
      command = u'psql %s -c "%s"' % (flags, query)
      if use_sudo:
          sudo(command, user='postgres', **kwargs)
      else:
          run(command, **kwargs)
docs: Execute remote psql query.