Thread: How to define variables in psql?

How to define variables in psql?

From
Date:


I know that one can store multiple psql commands and SQL statements in
a separate "script" file, and then "source" the script file, e.g. with
the \i command.

My question is, can I define variables in such a script file?
E.g. can I do something like the following unix-shell-like script?

-- beginning of script

DB=frobozz
DIR=/foo/bar/baz/quux

\c $frobozz
\i "$DIR/other_script.sql"
\i "$DIR/one_more_script.sql"
\i "$DIR/yas.sql"

\echo "done with $DB"

-- end of script

Thanks!

kj

Re: How to define variables in psql?

From
"A. Kretschmer"
Date:
am  31.01.2006, um  7:17:17 -0500 mailte kynn@panix.com folgendes:
>
>
>
> I know that one can store multiple psql commands and SQL statements in
> a separate "script" file, and then "source" the script file, e.g. with
> the \i command.
>
> My question is, can I define variables in such a script file?
> E.g. can I do something like the following unix-shell-like script?
>
> -- beginning of script
>
> DB=frobozz
> DIR=/foo/bar/baz/quux


something like this:?

\set DIR /home/kretschmer/
\set file :DIR foobar.sql

\set DB FOOBAR

\i :file

\echo "done with " :DB




HTH, Andreas
--
Andreas Kretschmer    (Kontakt: siehe Header)
Heynitz:  035242/47215,      D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
 ===    Schollglas Unternehmensgruppe    ===

Re: How to define variables in psql?

From
Date:

   From: A. Kretschmer <andreas.kretschmer@schollglas.com>
   Date: Jan 31, 2006 8:20 AM
   Subject: Re: [NOVICE] How to define variables in psql?
   To: pgsql-novice@postgresql.org

   am  31.01.2006, um  7:17:17 -0500 mailte kynn@panix.com folgendes:
   >
   > I know that one can store multiple psql commands and SQL statements in
   > a separate "script" file, and then "source" the script file, e.g. with
   > the \i command.
   >
   > My question is, can I define variables in such a script file?
   > E.g. can I do something like the following unix-shell-like script?
   >
   > -- beginning of script
   >
   > DB=frobozz
   > DIR=/foo/bar/baz/quux


   something like this:?

   \set DIR /home/kretschmer/
   \set file :DIR foobar.sql

   \set DB FOOBAR

   \i :file

   \echo "done with " :DB


Yes, exactly.  Thanks.

kj