title: Error when querying PostgreSQL using range operators
body: <p>I am trying to query a postgresql (v 9.3.6) table with a tstzrange to determine if a given timestamp exists within the table defined as</p>
  
  <pre><code>CREATE TABLE sensor(
    id serial,
    hostname varchar(64) NOT NULL,
    ip varchar(15) NOT NULL,
    period tstzrange NOT NULL,
    PRIMARY KEY(id),
    EXCLUDE USING gist (hostname WITH =, period with &amp;&amp;)
  ); 
  </code></pre>
  
  <p>I am using psycopg2 and when I try the query:</p>
  
  <pre><code>sql = "SELECT id FROM sensor WHERE %s &lt;@ period;"
  cursor.execute(sql,(isotimestamp,))
  </code></pre>
  
  <p>I get the error</p>
  
  <pre><code>psycopg2.DataError: malformed range literal: 
  ...
  DETAIL:  Missing left parenthesis or bracket.
  </code></pre>
  
  <p>I've tried various type castings to no avail.</p>
  
  <p>I've managed a workaround using the following query:</p>
  
  <pre><code>sql = "SELECT * FROM sensor WHERE %s BETWEEN lower(period) AND upper(period);"
  </code></pre>
  
  <p>but would like to know why I am having problem with the range operators. Is it my code or psycopg2 or what?</p>
  
  <p>Any help is appreciated.</p>
  
  <p>EDIT 1:
  In response to the comments, I have attempted the same query on a simple 1-row table in postgresql like below</p>
  
  <pre><code>=&gt; select * from sensor;
  session_id | hostname |    ip     |                              period                               
  ------------+----------+-----------+-------------------------------------------------------------------
  1 | bob    | 127.0.0.1 | ["2015-02-08 19:26:42.032637+00","2015-02-08 19:27:28.562341+00")
  </code></pre>
  
  <p>(1 row)</p>
  
  <p>Now by using the "@>" operator I get the following error:</p>
  
  <pre><code>=&gt; select * from sensor where period @&gt; '2015-02-08 19:26:43.04+00';
  ERROR:  malformed range literal: "2015-02-08 19:26:43.04+00"
  LINE 1: select * from sensor where period @&gt; '2015-02-08 19:26:42.03...
  </code></pre>
  
  <p>Which appears to be the same as the psycopg2 error, a malformed range literal, so I thought I would try typecasting to timestamp as below</p>
  
  <pre><code>=&gt; select * from sensor where sensor.period @&gt; '2015-02-08  19:26:42.032637+00'::timestamptz;
  session_id | hostname |    ip     |                              period                               
  </code></pre>
  
  <p>------------+----------+-----------+-------------------------------------------------------------------
      1 | feral    | 127.0.0.1 | ["2015-02-08 19:26:42.032637+00","2015-02-08 19:27:28.562341+00")</p>
  
  <p>So it appears that it is my mistake, the literal has to be typecast or it is assumed to be a range. Using psycopg2, the query can be executed with:</p>
  
  <pre><code>sql="select * from sensor where period @&gt; %s::timestamptz"    
  </code></pre>