--- email_registration.module.orig	2009-04-02 10:28:23.000000000 +1300
+++ email_registration.module	2009-04-02 10:29:09.000000000 +1300
@@ -11,11 +11,17 @@
     case 'insert':
     $namenew = preg_replace('/@.*$/', '', $edit['mail']);
     // if username generated from email record already exists, append underscore and number eg:(chris_123)
     if (db_result(db_query("SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
       // find the next number available to append to the name
-      $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
+      global $db_type;
+      if ($db_type == 'mysql') {
+        $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
+      }
+      else if ($db_type == 'pgsql') {
+        $sql = "SELECT CAST(COALESCE(SUBSTRING(name FROM '.+?_([0-9]+)$'), '0') AS INTEGER) AS nextidx FROM {users} WHERE name ~ '%s' ORDER BY nextidx DESC LIMIT 1";
+      }
       $nameidx = db_result(db_query($sql, '^'. $namenew .'_[0-9]+$'));
       $namenew .= '_'. ($nameidx + 1);
     }
     // replace with generated username
     db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid);
