title: PostgreSQL - filter database list
body: <p>I have this script (which backups databases daily):</p>
  
  <pre><code>#!/bin/bash
  # Location to place backups.
  backup_dir="/home/user/openerp/7.0/backup/"
  #String to append to the name of the backup files
  backup_date=`date +%Y-%m-%d`
  #Numbers of days you want to keep copie of your databases
  number_of_days=7
  databases=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
  for i in $databases; do
    if [ "$i" != "template0" ] &amp;&amp; [ "$i" != "template1" ]; then
      echo Dumping $i to $backup_dir$i\_$backup_date
      pg_dump -Fc $i &gt; $backup_dir$i\_$backup_date
    fi
  done
  find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;
  </code></pre>
  
  <p>When I run this script, it starts doing databases backups normally, but when it does like half database backups, it just hangs like it is doing some long backup and never ends it. So some of my databases some times end up not backed up.</p>
  
  <p>I think it is because it tries to backup databases like template0 and template1. I tried to look in documentation how this databases filtering is working, but didn't find any info.</p>
  
  <p>Can anybody tell me how to filter all my databases except databases like template0, template1, postgres. Also would be great if someone could give a link to documentation where it says about such filtering like this:</p>
  
  <pre><code>`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
  </code></pre>
  
  <p>Output as requested:</p>
  
  <pre><code>demo
  demo_empty1
  dn1
  dn2
  dn3
  da21
  da22
  nbb323
  nd
  nd2
  pf12
  postgres
  rub_demo1
  template0
  template1
  test
  test3
  testas_3
  </code></pre>
  
  <p>So all databases, except postgres, template0 and template1</p>