Thread: archive_command with an environnement variable ?

archive_command with an environnement variable ?

From
Eric Pailleau
Date:
Dear all,

In Postgresql documentation we have an example of the directive archive_command (for WAL) :

archive_command = 'cp -i %p /mnt/server/directory_archive/%f </dev/null'

or better :

archive_command = 'test ! -f /mnt/server/directory_archive/%f && cp %p /mnt/server/directory_archive/%f'

It look like this command is spawn to the system ( exec() or something... )

so I wonder if we can use environnement variable directly or by sourcing an env file, i.e :

archive_command = 'test ! -f $myvar/%f && cp %p $myvar/%f'
or
archive_command = 'csh ; source ~/.cshrc && test ! -f $myvar/%f && cp %p $myvar/%f'

Thanks for help.

Regards.



Re: archive_command with an environnement variable ?

From
Greg Smith
Date:
On Thu, 6 Sep 2007, Eric Pailleau wrote:

> so I wonder if we can use environnement variable directly or by sourcing an
> env file, i.e :
> archive_command = 'test ! -f $myvar/%f && cp %p $myvar/%f'
> archive_command = 'csh ; source ~/.cshrc && test ! -f $myvar/%f && cp %p
> $myvar/%f'

As far as I know these won't work, because PostgreSQL doesn't substitute
environment variables before making the system call.  There might be some
way to quote things and call a shell to make it happen, but I wasn't able
to find any simple syntax I was happy with.

The whole idea of doing all the processing within the archive_command is a
bad one anyway. You should write a script that gets passed %f and %p and
let it source environment variables and do all the testing.  It can return
multiple error codes when something goes wrong and anything it writes to
standard out shows up in the server log files, all of which makes
debugging and troubleshooting much easier.

The approach of the simple example is just that--a simple example; you
shouldn't consider it a model for how you should operate.  Having the code
in an external script gives you considerably more flexibility than trying
to squeeze everything into the archive_command.

--
* Greg Smith gsmith@gregsmith.com http://www.gregsmith.com Baltimore, MD