<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def build_query_cmd(self, query, csv=False):
 """ Builds the command needed to invoke the pgsql query as the postgres user. The query requires significant quoting work to satisfy both the shell and postgres parsing requirements. Note that this will generate a large amount of quoting in sos logs referencing the command being run """

        _cmd = "su postgres -c %s"
        if not csv:
            _dbcmd = "psql foreman -c %s"
        else:
            _dbcmd = "psql foreman -A -F , -X -c %s"
        dbq = _dbcmd % quote(query)
        return _cmd % quote(dbq)