Thread: Accessing environment variables from psql
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/
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/
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
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/