Both sql-scripts include the COPY (SELECT ...) TO STDOUT or COPY (SELECT ...) TO STDIN As a result nothing is copied.
When I run a much simpler version of the query with the -c "Select .." option it works. Because the sql-scripts are quite long, I don't to do it without the -f option.
As you pointed, my bet is in the -f case COPY FROM STDIN expects the data on the file ( otherwise pg_dumps would not work ), but your sugestion seems to have a problem of double redirection, let me elaborate:
folarte@paqueton:~$ echo aaaa > A folarte@paqueton:~$ echo bbbb | cat < A aaaa
( in this case the A file will simulate q2.sql, echo bbbb is simulating the first psql command and cat is simulating the second psql command ). You are redirecting the second psql input twice, one with | other with <. A simple variant is: