title: Dynamically inject the current date into PostgreSQL query with copy export
body: <p>Is there a way I can dynamically inject the date in YYYYMMDD format into this PostgreSQL query that I am using with <code>copy</code> inside <code>psql</code>?</p>
  
  <pre><code>copy (SELECT 
          course.id, course.area, course.status, course.type, 
          customer.firstname, customer.lastname, customer.email 
        FROM course 
        JOIN customer_course 
        ON customer_course.course_id = course.id 
        JOIN customer 
        ON customer.id = customer_course.customer_id 
        WHERE course.type LIKE '%heathland%' 
        AND course.status = 'open'
      ) TO '~/Dropbox/CRMPicco/prod-customer-courses-' . (SELECT NOW()::date) . '.csv' WITH CSV;
  </code></pre>
  
  <p>When I run that query I get the following error:</p>
  
  <pre><code>ERROR:  syntax error at or near "."
  LINE 1: ...ace%' AND course.status = 'open' ) TO STDOUT . (SELECT ...
  </code></pre>
  
  <p>It would be advantageous if I could create this CSV with the current date without having to manually edit the query.</p>