title: Postgresql 8.4->9.1 with pg_upgrade failing because database 'template0' exists
body: <p>I have been working to upgrade a Postgresql database from 8.4 to 9.1. I have encountered innumerable problems (may have to do with Ubuntu's file placements), but I think I see the finish line. 
  Pg_upgrade is almost working, but it fails when it reaches the first database, 'template0,' because it 'already exists.' </p>
  
  <pre><code>psql:/var/lib/postgresql/pg_upgrade_dump_globals.sql:36: ERROR:  database "template0" already exists
  
  There were problems executing "/usr/lib/postgresql/9.1/bin/psql" --set ON_ERROR_STOP=on --no-psqlrc --port 5432 --username "postgres" -f "/var/lib/postgresql/pg_upgrade_dump_globals.sql" --dbname template1 &gt;&gt; "/dev/null"
  Failure, exiting
  </code></pre>
  
  <p>This template0 database is of course part of every new cluster and can not be deleted. I tried renaming template0 and template1 (to be safe) on the new cluster (after recreating it), but the subsequent pg_upgrade call also failed, but this time complaining that template1 didn't exist on the new cluster. </p>
  
  <pre><code>"/usr/lib/postgresql/9.1/bin/pg_ctl" -w -l "/dev/null" -D "/var/lib/postgresql/9.1/main" -o "-p 5432 -b" start &gt;&gt; "/dev/null" 2&gt;&amp;1
  
  connection to database failed: FATAL:  database "template1" does not exist
  
  unable to connect to new postmaster started with the command: "/usr/lib/postgresql/9.1/bin/pg_ctl" -w -l "/dev/null" -D "/var/lib/postgresql/9.1/main" -o "-p 5432 -b" start &gt;&gt; "/dev/null" 2&gt;&amp;1
  Failure, exiting
  </code></pre>
  
  <p>I tried it again only renaming template0 and it failed again, but this time with some assertion issue from postgres itself.</p>
  
  <pre><code>executing: SELECT datcollate, datctype FROM     pg_catalog.pg_database WHERE    datname = 'template0' 
  
  pg_upgrade: /build/buildd/postgresql-9.1-9.1.3/build/../contrib/pg_upgrade/check.c:310: set_locale_and_encoding: Assertion `PQntuples(res) == 1' failed.
  Aborted (core dumped)
  </code></pre>
  
  <p>Sanity Check Notes: In case anyone thinks 'just do a full sql dump and restore it' that isn't working well either, which is why I am trying to get pg_upgrade working. I also made sure to drop and recreate the cluster every time an upgrade failed to prevent any relics of failures past from continuing to screw with the upgrade. The database is on an Amazon Volume, so I can't really break anything and always have a backup. I am on ubuntu 12 and have both databases installed side by side.</p>
  
  <p>I am currently out of ideas that doesn't involve going into the postgresql source and would greatly appreciate any help and suggestions as to what to try next. Thanks in advance.</p>