title: How to parameterize column names in a pg query
body: <p>I am trying to dynamically change the column queried in a WHERE statement.</p>
  <p>My code looks like this</p>
  <pre class="lang-js prettyprint-override"><code>const pg = require('pg');
  const pool = new pg.Pool();
  await pool.connect();
  
  let selector = {
    key: 'foo',
    value: 'bar',
  };
  
  const result = await pool.query(`SELECT * FROM foobar WHERE ${selector.key}=$1::text`, [selector.value]);
  </code></pre>
  <p>There is only a few key values I switch between. While the key is pre-defined and not from user input I would prefer not to use a template string.</p>
  <p>Is there a way to parameterize the column name?</p>
  <p>I have tried a few ways.</p>
  <ol>
  <li>Just using a default parameter</li>
  </ol>
  <pre class="lang-js prettyprint-override"><code>await pool.query('SELECT * FROM foobar WHERE $1=$2::text', [selector.key, selector.value]);
  </code></pre>
  <p>This doesn't work as it parameterizes the key as a string so is just doing a string comparison.</p>
  <ol start="2">
  <li>Casting as <code>oid</code> (this was mentioned somewhere but I can't remember where)</li>
  </ol>
  <pre class="lang-js prettyprint-override"><code>await pool.query('SELECT * FROM foobar WHERE $1::oid=$2::text', [selector.key, selector.value]);
  </code></pre>
  <p>This throws an error as pg doesn't have a way to cast text to <code>oid</code>.</p>