You seem to be attempting to wrap single quotes inside single quotes, which of course you cannot do. 'this'and'that'
parses into 'this'
followed by an unquoted and
followed by 'that'
, not into a quoted string this'and'that
.
The usual solution is to wrap single quotes in double quotes, or vice versa, so "this'and'that"
or 'this"and"that'
if substituting double quotes inside the quoted string is acceptable; but here, you already have both, so you can't do that (straightforwardly).
Assuming psql
can read commands from stdin, the simple workaround here is to use a here document.
su -c "psql -d myDB-c <<'____HERE'
SELECT count(*) AS number,
date_trunc('day'::text, users.registerdate) AS registerdate
FROM users;
____HERE" postgres