Declaring variable for a whole script
There are no global variables per se in Postgres. But we have "customized options" that can be streched for the puropose.

SET myvars.test TO '0.1';


Persists for the duration of the session (not just transaction). Use with:

SELECT current_setting('myvars.test')::float;


Be aware that values are stored as text. So you may need to cast as demonstrated.

To avoid exceptions when referencing an unset variable and get NULL instead:

SELECT current_setting('myvars.test', true)::float;


See:


Assign the result of current_setting(&#39;the_setting&#39;) to a variable in PL/pgSQL


There are a number of other workarounds, too:


Is there a way to define a named constant in a PostgreSQL query?