@andreasdueren one line at a time didn't work nor pasting it whole. I pasted them one section at a time and that worked. The "export" lines were done one at a time though. But the "echo" ones had to be as one. e.g.
echo "Checking for duplicate email templates..."
psql "$CLOUDRON_POSTGRESQL_URL" -v ON_ERROR_STOP=1 -c "
SELECT name, template_type, locale, account_id, inbox_id, COUNT(*)
FROM email_templates
GROUP BY name, template_type, locale, account_id, inbox_id
HAVING COUNT(*) > 1;
"
This was pasted in one go.