title: Cannot create plpgsql function using psql -f filename option
body: <p>I am using Postgres 8.4 and I have tried to run  a <code>create function ...</code>  script from the command line using</p>
  
  <pre><code>psql dbname -U username -f filename
  </code></pre>
  
  <p>or</p>
  
  <pre><code>psql -f filename -d dbname -U username 
  </code></pre>
  
  <p>and it always results in the following error</p>
  
  <blockquote>
    <p>psql:mergenodedata.sql:40: ERROR:  syntax error at or near "create"
    LINE 1: create or replace FUNCTION updNode (oldnodename varchar, ne...</p>
  </blockquote>
  
  <p>where line 40 is the end of the file</p>
  
  <pre><code>$$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>If I cut -and-paste file contents of the file into pgadmin or an open psql session, then the <code>create function</code> would perfectly.</p>
  
  <p>The code is</p>
  
  <pre><code>create or replace FUNCTION updNode (oldnodename varchar, newnodename varchar, scnname varchar, cid integer) returns void AS $$
  declare
          oldnodeid integer;
          newnodeid integer;
          scnid integer;
          newcount integer;
          oldcount integer;
  
  BEGIN
  
  raise notice 'doing %', oldnodename;
  
  select id from nodes where nodename = oldnodename and cityid = cid into oldnodeid;
  select id from nodes where nodename = newnodename and cityid = cid into newnodeid;
  select id from scenario where name = scnname and cityid = cid into scnid;
  
  raise notice 'oldnodeid is %', oldnodeid;
  raise notice 'newnodeid is %', newnodeid;
  raise notice 'scnid is %', scnid;
  
  select count(*) from collection_node_result where node1id = newnodeid and scenario_collection_id in (
          select id from scenario_collection where scenarioid = scnid into newcount
  );
  
  raise notice 'newcount is %', newcount;
  
  select count(*) from collection_node_result where node1id = oldnodeid and scenario_collection_id in (
          select id from scenario_collection where scenarioid = scnid into oldcount
  );
  
  raise notice 'oldcount is %', oldcount;
  
  
  update collection_node_result set node1id = newnodeid where node1id = oldnodeid and scenario_collection_id in (
          select id from scenario_collection where scenarioid = scnid
  );
  
  
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>Other pages I have refernced are</p>
  
  <p><a href="https://stackoverflow.com/questions/16559038/run-plpgsql-program-to-update-the-data-in-table">Run plpgsql program to update the data in table</a></p>
  
  <p><a href="http://www.postgresql.org/docs/8.4/static/plpgsql-development-tips.html" rel="nofollow noreferrer">http://www.postgresql.org/docs/8.4/static/plpgsql-development-tips.html</a></p>