title: How Do I Backup My PostgreSQL Database with Cron?
body: <p>I can run commands like vacuumdb, pg_dump, and psql just fine in a script if I preface them like so:</p>
  
  <pre><code>/usr/bin/sudo -u postgres /usr/bin/pg_dump -Fc mydatabase &gt; /opt/postgresql/prevac.gz
  /usr/bin/sudo -u postgres /usr/bin/vacuumdb --analyze mydatabase
  /usr/bin/sudo -u postgres /usr/bin/pg_dump -Fc mydatabase &gt; /opt/postgresql/postvac.gz
  SCHEMA_BACKUP="/opt/postgresql/$(date +%w).db.schema"
  sudo -u postgres /usr/bin/pg_dump -C -s mydatabase &gt; $SCHEMA_BACKUP
  </code></pre>
  
  <p>These run at command line on Redhat when I am sudo to root and then as you see in the commands above I do a sudo -u to postgres.</p>
  
  <p>But when I try to kick this off from cron, <strong>I get zero bytes in all the files -- meaning it didn't run properly.</strong> And I don't get a clue in the logs that I can see.</p>
  
  <p>My /etc/crontab file has this entry at the bottom</p>
  
  <pre><code>00 23 * * * root /etc/db_backup.cron
  </code></pre>
  
  <p>And yes, /etc/db_backup.cron is chmod ug+x, owned by root, and the top of the file says "#!/bin/bash" (minus doublequotes).</p>
  
  <p>Anyone know what gives?</p>