title: How can I quote a named argument passed in to psql?
body: <p><a href="http://www.postgresql.org/docs/9.1/static/app-psql.html" rel="noreferrer">psql</a> has a construct for passing named arguments:</p>
  
  <pre><code>psql -v name='value'
  </code></pre>
  
  <p>which can then be referenced inside a script:</p>
  
  <pre><code>SELECT :name;
  </code></pre>
  
  <p>which will give the result</p>
  
  <pre><code> ?column?
  ----------
   value
  (1 row)
  </code></pre>
  
  <p>During development, I need to drop and recreate copies of the database fairly frequently, so I'm trying to automate the process. So I need to run a query that forcibly disconnects all users and then drops the database. But the database this operates on will vary, so the database name needs to be an argument.</p>
  
  <p>The problem is that the query to disconnect the users requires a string (<code>WHERE pg_stat_activity.datname = 'dbname'</code>) and the query that drops requires an unquoted token (<code>DROP DATABASE IF EXISTS dbname</code>). (Sorry. Not sure what to call that kind of token.)</p>
  
  <p>I can use the named argument fine without quotes in the DROP query, but quoting the named argument in the disconnect query causes the argument to not be expanded. I.e., I would get the string <code>':name'</code> instead of the string <code>'value'</code>.</p>
  
  <p>Is there any way to turn the unquoted value into a string or turn a string into an unquoted token for the DROP query? I can work around it by putting the disconnect and DROP queries in separate scripts and passing the argument in with quotes to the disconnect and without quotes to the DROP, but I'd prefer they were in the same script since they're really two steps in a single process.</p>