This was just one way to define variables. Another is to indicate them in the command line, when running the script:
psql -v tabname=mytab2 -f vartest.sql
Variables can also be set interactively. The following line will prompt the user, and then set the variable to whatever is typed before hitting Enter:
\prompt 'Insert the table name: ' tabname
In the next recipe, we will see how to set variables using an SQL query.