title: postgres dblink escape single quote
body: <p>Related Link: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/935/string-literals-and-escape-characters-in-postgresql">String literals and escape characters in postgresql</a></li>
  </ul>
  
  <p>Here is my error:</p>
  
  <pre><code>ERROR:  type "e" does not exist
  </code></pre>
  
  <p>Here is my query:</p>
  
  <pre><code>SELECT *
  FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword',
      E'SELECT field_1, 
      CASE WHEN field_2 IS NOT NULL 
      THEN \'inactive\' ELSE \'active\' 
      END AS field_status 
      FROM the_table 
       ') 
  AS linkresults(field_1 varchar(20),field_2 varchar(8))
  </code></pre>
  
  <p>If I use double quotes, remove the backslash escape for the single quotes and remove the E before the SELECT statement </p>
  
  <pre><code>SELECT *
  FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword',
      "SELECT field_1, 
      CASE WHEN field_2 IS NOT NULL 
      THEN 'inactive' ELSE 'active' 
      END AS field_status 
      FROM the_table 
       ") 
  AS linkresults(field_1 varchar(20),field_2 varchar(8))
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>NOTICE:  identifier "SELECT ..." will be truncated
  </code></pre>
  
  <p>And the I also get the ERROR as my query has been truncated.</p>
  
  <p>I have escaped with dblink like this before, so is there a server setting or something I need to configure? </p>
  
  <p>I know the query works just fine if I run it on the sql server itself, but not with dblink. Any thoughts?</p>
  
  <p>Postgres version 8.4</p>