Вы можете просто plpgsql это:
db=# do $$
begin
if (select count(*) from t) > 0 then
copy t to '/tmp/t' with (format csv, header);
end if;
end;
$$
;
DO
db=# \! cat /tmp/t
c
2
db=# truncate table t;
TRUNCATE TABLE
db=# \! rm /tmp/t
db=# do $$
begin
if (select count(*) from t) > 0 then
copy t to '/tmp/t' with (format csv, header);
end if;
end;
$$
;
DO
db=# \! cat /tmp/t
cat: /tmp/t: No such file or directory