title: How to declare a variable in postgres script?
body: <p>I am fairly new to Postgres and I cannot believe how difficult I am finding just to declare a variable. I did come across other SO posts, but none of them helped in my situation. All I want is to write the a script like below in postgres:</p>
  
  <pre><code>declare @age int = 10;
  
  select * from person p where p.age &gt; @age;
  </code></pre>
  
  <p>Based on the SO post <a href="https://stackoverflow.com/questions/766657/how-do-you-use-variables-in-a-simple-postgresql-script">here</a>, I tried:</p>
  
  <pre><code>DO
  $$
      DECLARE
          overTheAgeOf int := 15;
      BEGIN
          select *
          from person
          where age &gt; overTheAgeOf;
      END
  $$;
  </code></pre>
  
  <p>This gives me error: <code>[42601] ERROR: query has no destination for result data</code></p>
  
  <p>Then I tried returning the result of the script:</p>
  
  <pre><code>return (select *
      from person
      where age &gt; overTheAgeOf);
  </code></pre>
  
  <p>That gave me another error: <code>ERROR: RETURN cannot have a parameter in function returning void</code></p>
  
  <p>How do declare a variable and use it in script(s) that follows?</p>