Re: Accessing environment variables from psql (SOLVED)

Lists: pgsql-general
From: "Leif B(dot) Kristensen" <leif(at)solumslekt(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Accessing environment variables from psql
Date: 2005-04-09 11:59:29
Message-ID: 200504091359.29549.leif@solumslekt.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Is there a way to access environment variables from psql? I can't find
any documentation on how to do this:

leif=> \! echo $IMPORTDIR
/home/leif/slekta/import/scripts
leif=> \i $IMPORTDIR/test.sql
$IMPORTDIR/test.sql: No such file or directory
leif=> \i $(IMPORTDIR)/test.sql
$(IMPORTDIR)/test.sql: No such file or directory
leif=> \i ${IMPORTDIR}/test.sql
${IMPORTDIR}/test.sql: No such file or directory
--
Leif Biberg Kristensen
http://solumslekt.org/


From: "Leif B(dot) Kristensen" <leif(at)solumslekt(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Accessing environment variables from psql (SOLVED)
Date: 2005-04-09 13:53:12
Message-ID: 200504091553.12528.leif@solumslekt.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Saturday 09 April 2005 13:59, Leif B. Kristensen wrote:
> Is there a way to access environment variables from psql?

After scrutinizing the psql documentation at
<http://www.postgresql.org/docs/8.0/static/app-psql.html>, I found that
this actually works:

leif=> \set importdir `echo $IMPORTDIR`
leif=> \echo :importdir
/home/leif/slekta/import/scripts

This doesn't:

leif=> \i :importdir/test.sql
\i: extra argument "/test.sql" ignored

But this does:

leif=> \cd :importdir
leif=> \i test.sql

So, the problem is solved, sort of. It may also be prudent to save the
old pwd and return there when the work is done:

leif=> \set olddir `echo $PWD`
leif=> \set importdir `echo $IMPORTDIR`
leif=> \cd :importdir
leif=> \i test.sql
leif=> \cd :olddir
--
Leif Biberg Kristensen
http://solumslekt.org/


From: Andreas Seltenreich <seltenreich(at)gmx(dot)de>
To: "Leif B(dot) Kristensen" <leif(at)solumslekt(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Accessing environment variables from psql (SOLVED)
Date: 2005-04-10 01:39:08
Message-ID: 874qef1kcj.fsf@gate450.dyndns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Leif B. Kristensen writes:

> So, the problem is solved, sort of. It may also be prudent to save the
> old pwd and return there when the work is done:
>
> leif=> \set olddir `echo $PWD`
> leif=> \set importdir `echo $IMPORTDIR`
> leif=> \cd :importdir
> leif=> \i test.sql
> leif=> \cd :olddir

You can concatenate values using \set. So using

\set importfile :importdir /test.sql
\i :importfile

you won't have to mess with $PWD.

regards,
Andreas


From: "Leif B(dot) Kristensen" <leif(at)solumslekt(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Accessing environment variables from psql (SOLVED)
Date: 2005-04-10 20:28:53
Message-ID: 200504102228.54001.leif@solumslekt.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Sunday 10 April 2005 03:39, Andreas Seltenreich wrote:

> You can concatenate values using \set. So using
>
> \set importfile :importdir /test.sql
> \i :importfile
>
> you won't have to mess with $PWD.

Thanks for the tip, Andreas. But I have about a dozen sql files in the
import directory, one for each table. So it's actually easier to do a
cd there and run the import, than to concatenate the pathname to every
single file name.

However, running an install script from within psql may be the wrong
approach. I might as well define and populate the database from a shell
script.
--
Leif Biberg Kristensen
http://solumslekt.org/