title: postgres backup script not as postgres user
body: <p>I want to create a postgres backup script but I don't want to use the postgres user because the unix system I am in has few restrictions. What I want to do is run this script as a regular user of the unix system (network) on a crontab and use database admin account in the script.</p>
  
  <p>Just to make it clear in the unix system (network) I have user <code>foo</code>, now we have <code>postgres</code> user who is default postgres user, and then for the database we have a database admin user <code>my_db_admin</code>. I script to execute as network user <code>foo</code> using <code>my_db_admin</code> user and credentials in the script. </p>
  
  <p>This is what I started with but the problem is when I execute the script it gives an error saying password was not provided.</p>
  
  <pre><code>#!/bin/bash
  export PASSWORD="MyPassword"
  backup_dir="/BACKUP/LOCATION"
  # name of the backup file has the date
  backup_date=`date +%d-%m-%Y`
  # only keep the backup for 30 days (maintain low storage)
  number_of_days=30
  pg_dump -Fc -Umy_db_admin MyDatabase -w &gt; $backup_dir\_$backup_date
  find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;
  </code></pre>
  
  <p>Here is the error: </p>
  
  <pre><code>pg_dump: [archiver (db)] connection to database "MyDatabase" failed: fe_sendauth: no password supplied
  </code></pre>