Jessica
Jessica

Reputation: 2415

Postgres dump command works on the shell, but not when I add it to a crontab

I've been trying to run the command

pg_dump -U database_username database_name > /path/to/directory/database_name.$(date +"%Y-%m-%d-%H:%M:%S").dump

And this command works when I do it from the command prompt, but it doesn't output anything when I put it in a crontab. Why is that?

I also defined a .pgpass file in my home directory, which contains lines like

hostname:port:database_name:database_username:database_password

so when I run the command from the command prompt, it doesn't prompt me for a password. I'm not sure if the crontab has access to that file or not.

Upvotes: 0

Views: 451

Answers (2)

Mizael Clistion
Mizael Clistion

Reputation: 101

#!/bin/bash

export PGPASSFILE="/home/YOUR_LINUX_USER/.pg_pass"

... the rest of the script

Upvotes: 0

J. Chomel
J. Chomel

Reputation: 8395

If you want to run your script as if you where connected directly to the server from cron, you should source for your env. variables to be set. E.g. say you're running on ksh with pg_usr user:

10 03 * * * source /home/pg_usr/.kshrc ; my_postgre_shell

Another thing is cron might not set the same umask as defaulted when you connect directly, resulting in different file access rights on the files generated by the cron session. So you must set also the desired umask:

10 03 * * * source /home/pg_usr/.kshrc ; umask 002 ; my_postgre_shell 

Upvotes: 1

Related Questions