how to split long psql command in bash script over several lines
Nothing wrong with splitting it up with backlashes as you showed. However, it's generally better to send the SQL via stdin. For postgres, this is especially true, since the '-c' option is fixed to return output from only one command, whereas accepting commands from stdin, you can stack as many commands together as you like. So, you would do something like:
sudo -u postgres /opt/puppet/bin/psql puppetdb -t <<SQL | ...
select certname, r.value as role, e.value as env
from certname_facts r join certname_faces e using (certname)
where r.name = 'role' and e.name = 'env'
order by role,env,certname
SQL
Bash variables might get interpolated here. To avoid that, quote the first instance of SQL
:
sudo -u postgres /opt/puppet/bin/psql puppetdb -t <<'SQL' | ...