diff -ruwB feed-on-feeds-svn/classes/fof-prefs.php feed-on-feeds-mdb2_pgsql/classes/fof-prefs.php
--- feed-on-feeds-svn/classes/fof-prefs.php	2008-02-24 20:52:38.000000000 +0100
+++ feed-on-feeds-mdb2_pgsql/classes/fof-prefs.php	2008-03-26 15:13:41.000000000 +0100
@@ -25,7 +25,7 @@
 		$this->user_id = $user_id;
 
         $result = fof_safe_query("select user_prefs from $FOF_USER_TABLE where user_id = %d", $user_id);
-        $row = mysql_fetch_array($result);
+        $row = $result -> fetchRow();
         $prefs = unserialize($row['user_prefs']);
         if(!is_array($prefs)) $prefs = array();
         $this->prefs = $prefs;
@@ -33,7 +33,7 @@
         if($user_id != 1)
         {
             $result = fof_safe_query("select user_prefs from $FOF_USER_TABLE where user_id = 1");
-            $row = mysql_fetch_array($result);
+            $row = $result -> fetchRow();
             $admin_prefs = unserialize($row['user_prefs']);
             if(!is_array($admin_prefs)) $admin_prefs = array();
             $this->admin_prefs = $admin_prefs;
diff -ruwB feed-on-feeds-svn/fof-config-sample.php feed-on-feeds-mdb2_pgsql/fof-config-sample.php
--- feed-on-feeds-svn/fof-config-sample.php	2008-02-24 20:52:39.000000000 +0100
+++ feed-on-feeds-mdb2_pgsql/fof-config-sample.php	2008-02-24 19:39:01.000000000 +0100
@@ -15,6 +15,7 @@
 
 // Database connection information.  Host, username, password, database name.
 
+define('FOF_DB_BACKEND', "pgsql"); // pgsql, mysql, ...
 define('FOF_DB_HOST', "host.example.com");
 define('FOF_DB_USER', "username");
 define('FOF_DB_PASS', "password");
diff -ruwB feed-on-feeds-svn/fof-db.php feed-on-feeds-mdb2_pgsql/fof-db.php
--- feed-on-feeds-svn/fof-db.php	2008-02-24 20:52:39.000000000 +0100
+++ feed-on-feeds-mdb2_pgsql/fof-db.php	2008-03-26 15:23:35.000000000 +0100
@@ -12,6 +12,8 @@
  *
  */
 
+include_once('MDB2.php');
+
 $FOF_FEED_TABLE = FOF_FEED_TABLE;
 $FOF_ITEM_TABLE = FOF_ITEM_TABLE;
 $FOF_ITEM_TAG_TABLE = FOF_ITEM_TAG_TABLE;
@@ -27,23 +29,35 @@
 {
     global $fof_connection;
     
-    $fof_connection = mysql_connect(FOF_DB_HOST, FOF_DB_USER, FOF_DB_PASS) or die("<br><br>Cannot connect to database.  Please update configuration in <b>fof-config.php</b>.  Mysql says: <i>" . mysql_error() . "</i>");
-    mysql_select_db(FOF_DB_DBNAME, $fof_connection) or die("<br><br>Cannot select database.  Please update configuration in <b>fof-config.php</b>.  Mysql says: <i>" . mysql_error() . "</i>");
+    $fof_connection = MDB2::connect(FOF_DB_BACKEND."://".FOF_DB_USER.":".FOF_DB_PASS."@".FOF_DB_HOST."/".FOF_DB_DBNAME);
+    if (PEAR::isError($fof_connection)) {
+            die("Cannot connect to the SQL. <br /> SQL says: " . $fof_connection->getMessage() . " (" . $fof_connection->getCode() . ")");
+    }
+
+    $fof_connection->setFetchMode(MDB2_FETCHMODE_ASSOC);
+
+    // Manager needed to vacuum/optimize the database and to alter it (during install step)
+    $fof_connection->loadModule('Manager', null, true);
 }
 
 function fof_db_optimize()
 {
-	global $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_TAG_TABLE, $FOF_USER_TABLE;
+    global $fof_connection;
     
-	fof_db_query("optimize table $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_TAG_TABLE, $FOF_USER_TABLE");
+    // vacuum needs >=Pear::MDB2-1.5.0_beta1
+    if (Pear::isError($fof_connection->manager->vacuum(null, array(analyze=>true)))) {
+            die("Cannot vacuum database. SQL says: <b>". $result->getMessage() ." (" . $result->getCode() . ")</b> - " . $result->getUserinfo());
+    }
 }
 
 function fof_safe_query(/* $query, [$args...]*/)
 {
+    global $fof_connection;
+
     $args  = func_get_args();
     $query = array_shift($args);
     if(is_array($args[0])) $args = $args[0];
-    $args  = array_map('mysql_real_escape_string', $args);
+    $args  = array_map('addslashes', $args);
     $query = vsprintf($query, $args);
     
     return fof_db_query($query);
@@ -56,10 +70,12 @@
     list($usec, $sec) = explode(" ", microtime()); 
     $t1 = (float)$sec + (float)$usec;
     
-    $result = mysql_query($sql, $fof_connection);
+    $result = $fof_connection->query($sql);
+
     
-    if(is_resource($result)) $num = mysql_num_rows($result);
-    if($result) $affected = mysql_affected_rows();
+    if($result && !PEAR::isError($result)) $num = ($result->numRows());
+    // No affectedRows method in PEAR::MDB2
+    //if($result) $affected = $fof_connection->affectedRows();
     
     list($usec, $sec) = explode(" ", microtime()); 
     $t2 = (float)$sec + (float)$usec;
@@ -73,12 +89,12 @@
     }
     else
     {
-        if(mysql_errno()) 
+        if(PEAR::isError($result)) 
         {
             //echo "<pre>";
             //print_r(debug_backtrace());
             //echo "</pre>";
-            die("Cannot query database.  Have you run <a href=\"install.php\"><code>install.php</code></a> to create or upgrade your installation? MySQL says: <b>". mysql_error() . "</b>");
+            die("Cannot query database. Have you run <a href=\"install.php\"><code>install.php</code></a> to create or upgrade your installation? <br /> SQL says: <b>". $result->getMessage() ." (" . $result->getCode() . ")</b> - " . $result->getUserinfo());
         }
         return $result;
     }
@@ -86,7 +102,7 @@
 
 function fof_db_get_row($result)
 {
-    return mysql_fetch_array($result);
+    return $result->fetchRow();
 }
 
 
@@ -190,7 +206,7 @@
     
     $result = fof_safe_query("select $FOF_SUBSCRIPTION_TABLE.feed_id from $FOF_FEED_TABLE, $FOF_SUBSCRIPTION_TABLE where feed_url='%s' and $FOF_SUBSCRIPTION_TABLE.feed_id = $FOF_FEED_TABLE.feed_id and $FOF_SUBSCRIPTION_TABLE.user_id = %d", $feed_url, $user_id);
     
-    if(mysql_num_rows($result) == 0)
+    if($result->numRows() == 0)
     {
         return false;
     }
@@ -204,12 +220,12 @@
     
     $result = fof_safe_query("select * from $FOF_FEED_TABLE where feed_url='%s'", $feed_url);
     
-    if(mysql_num_rows($result) == 0)
+    if($result->numRows() == 0)
     {
         return NULL;
     }
     
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
     return $row;
 }
@@ -220,7 +236,7 @@
     
     $result = fof_safe_query("select * from $FOF_FEED_TABLE where feed_id=%d", $feed_id);
     
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
     return $row;
 }
@@ -231,7 +247,11 @@
     
     fof_safe_query("insert into $FOF_FEED_TABLE (feed_url,feed_title,feed_link,feed_description) values ('%s', '%s', '%s', '%s')", $url, $title, $link, $description);
     
-    return(mysql_insert_id($fof_connection));
+    $id = $fof_connection->lastInsertID($FOF_FEED_TABLE,'feed_id');
+    if (PEAR::isError($id)) {
+            die($id->getMessage());
+    }
+    return $id;
 }
 
 function fof_db_add_subscription($user_id, $feed_id)
@@ -277,9 +297,9 @@
     global $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_SUBSCRIPTION_TABLE, $fof_connection;
     
     $result = fof_safe_query("select item_id from $FOF_ITEM_TABLE where feed_id=%d and item_guid='%s'", $feed_id, $item_guid);
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
-    if(mysql_num_rows($result) == 0)
+    if($result->numRows() == 0)
     {
         return NULL;
     }
@@ -296,7 +316,11 @@
     fof_safe_query("insert into $FOF_ITEM_TABLE (feed_id, item_link, item_guid, item_title, item_content, item_cached, item_published, item_updated) values (%d, '%s', '%s' ,'%s', '%s', %d, %d, %d)",
     $feed_id, $link, $guid, $title, $content, $cached, $published, $updated);
     
-    return(mysql_insert_id($fof_connection));
+    $id = $fof_connection->lastInsertID($FOF_ITEM_TABLE,'item_id');
+    if (PEAR::isError($id)) {
+            die($id->getMessage());
+    }
+    return $id;
 }
 
 function fof_db_get_items($user_id=1, $feed=NULL, $what="unread", $when=NULL, $start=NULL, $limit=NULL, $order="desc", $search=NULL)
@@ -329,7 +353,7 @@
             $limit = $prefs["howmany"];
         }
         
-        $limit_clause = " limit $start, $limit ";
+        $limit_clause = " offset $start limit $limit ";
     }
     
     $args = array();
@@ -354,7 +378,7 @@
         $from .= ", $FOF_TAG_TABLE t, $FOF_ITEM_TAG_TABLE it ";
         $where .= sprintf("AND it.user_id = %d ", $user_id);
         $where .= "AND it.tag_id = t.tag_id AND ( t.tag_name IN ( $in ) ) AND i.item_id = it.item_id ";
-        $group = sprintf("GROUP BY i.item_id HAVING COUNT( i.item_id ) = %d ", count($tags));
+        $group = sprintf("GROUP BY f.feed_cache, f.feed_cache_attempt_date, f.feed_cache_date, f.feed_description, f.feed_id, f.feed_id, f.feed_image, f.feed_image_cache_date, f.feed_link, f.feed_title, f.feed_url, i.item_cached, i.item_content, i.item_guid, i.item_id, i.item_link, i.item_published, i.item_title, i.item_updated, i.feed_id HAVING COUNT( i.item_id ) = %d ", count($tags));
         $args = array_merge($args, $tags);
     }
     
@@ -371,12 +395,12 @@
     
     $result = fof_safe_query($query, $args);
     
-    if(mysql_num_rows($result) == 0)
+    if($result->numRows() == 0)
     {
         return array();
     }
     	
-    while($row = mysql_fetch_assoc($result))
+    while($row = $result->fetchRow())
     {
         $array[] = $row;
     }
@@ -416,7 +440,7 @@
     
     $result = fof_safe_query($query, $item_id);
     
-    $item = mysql_fetch_assoc($result);
+    $item = $result->fetchRow();
     
     $item['tags'] = array();
     
@@ -496,7 +520,7 @@
     global $FOF_TAG_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $fof_connection;
     
     $result = fof_safe_query("select count(*) as \"count\" from $FOF_ITEM_TAG_TABLE where item_id=%d and tag_id <= 2", $item_id);
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
     return $row["count"];
 }
@@ -506,7 +530,7 @@
     global $FOF_ITEM_TAG_TABLE;
     
     $result = fof_safe_query("select count(*) as \"count\" from $FOF_ITEM_TAG_TABLE where tag_id = 1 and user_id = %d", $user_id); 
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
     return $row["count"];
 }
@@ -534,7 +558,7 @@
         FROM $FOF_TAG_TABLE
         LEFT JOIN $FOF_ITEM_TAG_TABLE ON $FOF_TAG_TABLE.tag_id = $FOF_ITEM_TAG_TABLE.tag_id
         WHERE $FOF_ITEM_TAG_TABLE.user_id = %d
-        GROUP BY $FOF_TAG_TABLE.tag_id order by $FOF_TAG_TABLE.tag_name";
+        GROUP BY $FOF_TAG_TABLE.tag_id,$FOF_TAG_TABLE.tag_name order by $FOF_TAG_TABLE.tag_name";
     
     $result = fof_safe_query($sql, $user_id);
     
@@ -565,7 +589,11 @@
     
     fof_safe_query("insert into $FOF_TAG_TABLE (tag_name) values ('%s')", $tag);
     
-    return(mysql_insert_id($fof_connection));
+    $id = $fof_connection->lastInsertID($FOF_TAG_TABLE,'tag_id');
+    if (PEAR::isError($id)) {
+            die($id->getMessage());
+    }
+    return $id;
 }
 
 function fof_db_get_tag_by_name($user_id, $tag)
@@ -574,12 +602,12 @@
     
     $result = fof_safe_query("select $FOF_TAG_TABLE.tag_id from $FOF_TAG_TABLE where $FOF_TAG_TABLE.tag_name = '%s'", $tag);
     
-    if(mysql_num_rows($result) == 0)
+    if($result->numRows() == 0)
     {
         return NULL;
     }
     
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
     return $row['tag_id'];
 }
@@ -639,18 +667,9 @@
     
     foreach($users as $user)
     {
-        $sql[] = sprintf("(%d, 1, %d)", $user, $id);
-    }
-    
-    $values = implode ( ",", $sql );
-    
+        $values = sprintf("(%d, 1, %d)", $user, $id);
 	$sql = "insert into $FOF_ITEM_TAG_TABLE (user_id, tag_id, item_id) values " . $values;
-	
-	$result = fof_db_query($sql, 1);
-
-    if(!$result && (mysql_errno() != 1062))
-    {
-        die("Cannot query database.  Have you run <a href=\"install.php\"><code>install.php</code></a> to create or upgrade your installation? MySQL says: <b>". mysql_error() . "</b>");
+        $result = fof_db_query($sql);
     }
 }
 
@@ -664,18 +683,9 @@
 
     foreach($items as $item)
     {
-        $sql[] = sprintf("(%d, %d, %d)", $user_id, $tag_id, $item);
-    }
-    
-    $values = implode ( ",", $sql );
-    
+        $values = sprintf("(%d, %d, %d)", $user_id, $tag_id, $item);
 	$sql = "insert into $FOF_ITEM_TAG_TABLE (user_id, tag_id, item_id) values " . $values;
-	
-	$result = fof_db_query($sql, 1);
-    
-    if(!$result && (mysql_errno() != 1062))
-    {
-        die("Cannot query database.  Have you run <a href=\"install.php\"><code>install.php</code></a> to create or upgrade your installation? MySQL says: <b>". mysql_error() . "</b>");
+        $result = fof_db_query($sql);
     }
 }
 
@@ -743,7 +753,7 @@
 {
     global $FOF_USER_TABLE;
     $result = fof_safe_query("select user_id from $FOF_USER_TABLE where user_name = '%s'", $username);
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
     return $row['user_id'];
 }
@@ -771,14 +781,15 @@
 {
     global $FOF_USER_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $fof_connection, $fof_user_id, $fof_user_name, $fof_user_level;
     
+    
     $result = fof_safe_query("select * from $FOF_USER_TABLE where user_name = '%s' and user_password_hash = '%s'", $user_name, $user_password_hash);
     
-    if(mysql_num_rows($result) == 0)
+    if($result -> numRows() == 0)
     {
         return false;
     }
     
-    $row = mysql_fetch_array($result);
+    $row = $result->fetchRow();
     
     $fof_user_name = $row['user_name'];
     $fof_user_id = $row['user_id'];
diff -ruwB feed-on-feeds-svn/fof-main.php feed-on-feeds-mdb2_pgsql/fof-main.php
--- feed-on-feeds-svn/fof-main.php	2008-02-24 20:52:39.000000000 +0100
+++ feed-on-feeds-mdb2_pgsql/fof-main.php	2008-03-26 15:12:51.000000000 +0100
@@ -511,7 +511,7 @@
 {
     fof_db_delete_subscription($user_id, $feed_id);
     
-    if(mysql_num_rows(fof_get_subscribed_users($feed_id)) == 0)
+    if(fof_get_subscribed_users($feed_id)->numRows() == 0)
     {
     	fof_db_delete_feed($feed_id);
     }
@@ -903,7 +903,7 @@
             $in = implode ( ", ", $ids );
             
             global $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE;
-            $sql = "select item_id, item_cached from $FOF_ITEM_TABLE where feed_id = $feed_id and item_id not in ($in) order by item_cached desc limit $count, 1000000000";
+            $sql = "select item_id, item_cached from $FOF_ITEM_TABLE where feed_id = $feed_id and item_id not in ($in) order by item_cached desc offset $countlimit 1000000000";
             $result = fof_db_query($sql);
             
             while($row = fof_db_get_row($result))
diff -ruwB feed-on-feeds-svn/install.php feed-on-feeds-mdb2_pgsql/install.php
--- feed-on-feeds-svn/install.php	2008-02-24 20:52:39.000000000 +0100
+++ feed-on-feeds-mdb2_pgsql/install.php	2008-03-26 15:20:38.000000000 +0100
@@ -12,12 +12,11 @@
  *
  */
 
+@include_once('MDB2.php');
+
 $fof_no_login = true;
 $fof_installer = true;
 
-include_once("fof-main.php");
-
-fof_set_content_type();
 
 // compatibility testing code lifted from SimplePie
 
@@ -38,10 +37,40 @@
         return $curl;
 }
 
+
+// Test if a field is present in the given table
+function isFieldPresent($table, $field) {
+    $present=false;
+    global $fof_connection;
+
+    $fields = $fof_connection -> manager -> listTableFields($table);
+
+    if (PEAR::isError($fields)) {
+        exit ("Error while testing for field presence. SQL says: " . $fields->getMessage() . "<br />");
+    }
+
+    foreach($fields as $f => $value) {
+        if ( $value == $field) $present=true;
+    }
+
+    return $present;
+}
+
+// Alter table
+function alterTable($table, $changes) {
+    global $fof_connection;
+
+    $result = $fof_connection -> manager -> alterTable($table, $changes, false);
+
+    if (PEAR::isError($result)) {
+        exit ("Error while altering table. SQL says: " . $result->getMessage() . "<br />");
+    }
+}
+
 $php_ok = (function_exists('version_compare') && version_compare(phpversion(), '4.3.2', '>='));
 $xml_ok = extension_loaded('xml');
 $pcre_ok = extension_loaded('pcre');
-$mysql_ok = extension_loaded('mysql');
+$sql_ok = class_exists('MDB2');
 
 $curl_ok = (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>='));
 $zlib_ok = extension_loaded('zlib');
@@ -96,6 +125,10 @@
 <?php
 if($_GET['password'] && $_GET['password'] == $_GET['password2'] )
 {
+    include_once("fof-main.php");
+
+    fof_set_content_type();
+
 	$password_hash = md5($_GET['password'] . 'admin');
 	fof_safe_query("insert into $FOF_USER_TABLE (user_id, user_name, user_password_hash, user_level) values (1, 'admin', '%s', 'admin')", $password_hash);
 	
@@ -136,10 +169,10 @@
     exit;
 }
 
-if($mysql_ok) echo "<span class='pass'>MySQL ok...</span> ";
+if($sql_ok) echo "<span class='pass'>SQL Backend ok...</span> ";
 else
 {
-    echo "<br><span class='fail'>Your PHP installation is missing the MySQL extension!</span>  This is required by Feed on Feeds.  Sorry!";
+    echo "<br><span class='fail'>Your PHP installation is missing the PEAR::MDB2 extension!</span>  This is required by Feed on Feeds.  Sorry!";
     echo "</div></body></html>";
     exit;
 }
@@ -175,83 +208,94 @@
 Creating tables...
 <?php
 
+// Now that requirements are checked, load everything
+include_once("fof-main.php");
+
+// module nedeed to test field presence in tables
+//$fof_connection->loadModule('Manager', null, true);
+
+//fof_set_content_type();
+
+
+
 $tables[] = <<<EOQ
-CREATE TABLE IF NOT EXISTS `$FOF_FEED_TABLE` (
-  `feed_id` int(11) NOT NULL auto_increment,
-  `feed_url` text NOT NULL,
-  `feed_title` text NOT NULL,
-  `feed_link` text NOT NULL,
-  `feed_description` text NOT NULL,
-  `feed_image` text,
-  `feed_image_cache_date` int(11) default '0',
-  `feed_cache_date` int(11) default '0',
-  `feed_cache_attempt_date` int(11) default '0',
-  `feed_cache` text,
-  PRIMARY KEY  (`feed_id`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+CREATE TABLE $FOF_FEED_TABLE (
+  feed_id serial NOT NULL,
+  feed_url text NOT NULL,
+  feed_title text NOT NULL,
+  feed_link text NOT NULL,
+  feed_description text NOT NULL,
+  feed_image text,
+  feed_image_cache_date numeric(11) default '0',
+  feed_cache_date numeric(11) default '0',
+  feed_cache_attempt_date numeric(11) default '0',
+  feed_cache text,
+  PRIMARY KEY  (feed_id)
+);
 EOQ;
 
 $tables[] = <<<EOQ
-CREATE TABLE IF NOT EXISTS `$FOF_ITEM_TABLE` (
-  `item_id` int(11) NOT NULL auto_increment,
-  `feed_id` int(11) NOT NULL default '0',
-  `item_guid` text NOT NULL,
-  `item_link` text NOT NULL,
-  `item_cached` int(11) NOT NULL default '0',
-  `item_published` int(11) NOT NULL default '0',
-  `item_updated` int(11) NOT NULL default '0',
-  `item_title` text NOT NULL,
-  `item_content` text NOT NULL,
-  PRIMARY KEY  (`item_id`),
-  KEY `feed_id` (`feed_id`),
-  KEY `item_guid` (`item_guid`(255)),
-  KEY `feed_id_item_cached` (`feed_id`,`item_cached`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+CREATE TABLE $FOF_ITEM_TABLE (
+  item_id serial NOT NULL,
+  feed_id numeric(11) NOT NULL default '0',
+  item_guid text NOT NULL,
+  item_link text NOT NULL,
+  item_cached numeric(11) NOT NULL default '0',
+  item_published numeric(11) NOT NULL default '0',
+  item_updated numeric(11) NOT NULL default '0',
+  item_title text NOT NULL,
+  item_content text NOT NULL,
+  PRIMARY KEY  (item_id)
+);
 EOQ;
 
 $tables[] = <<<EOQ
-CREATE TABLE IF NOT EXISTS `$FOF_ITEM_TAG_TABLE` (
-  `user_id` int(11) NOT NULL default '0',
-  `item_id` int(11) NOT NULL default '0',
-  `tag_id` int(11) NOT NULL default '0',
-  PRIMARY KEY  (`user_id`,`item_id`,`tag_id`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+CREATE TABLE $FOF_ITEM_TAG_TABLE (
+  user_id numeric(11) NOT NULL default '0',
+  item_id numeric(11) NOT NULL default '0',
+  tag_id numeric(11) NOT NULL default '0',
+  PRIMARY KEY  (user_id,item_id,tag_id)
+);
 EOQ;
 
 $tables[] = <<<EOQ
-CREATE TABLE IF NOT EXISTS `$FOF_SUBSCRIPTION_TABLE` (
-  `feed_id` int(11) NOT NULL default '0',
-  `user_id` int(11) NOT NULL default '0',
-  `subscription_prefs` text,
-  PRIMARY KEY  (`feed_id`,`user_id`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+CREATE TABLE $FOF_SUBSCRIPTION_TABLE (
+  feed_id numeric(11) NOT NULL default '0',
+  user_id numeric(11) NOT NULL default '0',
+  subscription_prefs text,
+  PRIMARY KEY  (feed_id,user_id)
+);
 EOQ;
 
 $tables[] = <<<EOQ
-CREATE TABLE IF NOT EXISTS `$FOF_TAG_TABLE` (
-  `tag_id` int(11) NOT NULL auto_increment,
-  `tag_name` char(100) NOT NULL default '',
-  PRIMARY KEY  (`tag_id`),
-  UNIQUE KEY (`tag_name`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+CREATE TABLE $FOF_TAG_TABLE (
+  tag_id serial NOT NULL,
+  tag_name char(100) NOT NULL default '',
+  PRIMARY KEY  (tag_id),
+  UNIQUE (tag_name)
+);
 EOQ;
 
 $tables[] = <<<EOQ
-CREATE TABLE IF NOT EXISTS `$FOF_USER_TABLE` (
-  `user_id` int(11) NOT NULL auto_increment,
-  `user_name` varchar(100) NOT NULL default '',
-  `user_password_hash` varchar(32) NOT NULL default '',
-  `user_level` enum('user','admin') NOT NULL default 'user',
-  `user_prefs` text,
-  PRIMARY KEY  (`user_id`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+CREATE TABLE $FOF_USER_TABLE (
+  user_id serial NOT NULL,
+  user_name varchar(100) NOT NULL default '',
+  user_password_hash varchar(32) NOT NULL default '',
+  user_level varchar(5) NOT NULL default 'user',
+  user_prefs text,
+  PRIMARY KEY  (user_id),
+  CHECK (user_level IN ('user', 'admin'))
+);
 EOQ;
 
+//  user_password_hash varchar(32) NOT NULL default '',
 foreach($tables as $table)
 {
-	if(!fof_db_query($table, 1))
+    // Check if there was an error while creating table
+    // Error code -5 occures when tables already exists
+    if(PEAR::isError($result=fof_db_query($table, 1)) && $result->getCode() != -5 )
 	{
-		exit ("Can't create table.  MySQL says: <b>" . mysql_error() . "</b><br>" );
+        exit ("Can't create table. SQL says: <b>" .  $result->getMessage() ." (" . $result->getCode() . ")</b> - ". $result->getUserinfo()  . "<br />" );
 	}
 }
 
@@ -258,15 +302,22 @@
 ?>
 Tables exist.<hr>
 
+
+
 <?php
-$result = fof_db_query("show columns from $FOF_FEED_TABLE like 'feed_image_cache_date'");
 
-if(mysql_num_rows($result) == 0)
+if (!isFieldPresent("$FOF_FEED_TABLE", 'feed_image_cache_date'))
 {
 
 print "Upgrading schema...";
 
-fof_db_query("ALTER TABLE $FOF_FEED_TABLE ADD `feed_image_cache_date` INT( 11 ) DEFAULT '0' AFTER `feed_image` ;");
+alterTable($FOF_FEED_TABLE, 
+    array('add' => array( 
+        'feed_image_cache_date' => array(
+            'type' => 'integer',
+            'length' => 11,
+            'default' => '0'
+        ))));
 
 print "Done.<hr>";
 }
@@ -274,14 +325,19 @@
 
 
 <?php
-$result = fof_db_query("show columns from $FOF_USER_TABLE like 'user_password_hash'");
 
-if(mysql_num_rows($result) == 0)
+if (!isFieldPresent("$FOF_USER_TABLE", 'user_password_hash'))
 {
 
 print "Upgrading schema...";
 
-fof_db_query("ALTER TABLE $FOF_USER_TABLE CHANGE `user_password` `user_password_hash` VARCHAR( 32 ) NOT NULL");
+alterTable($FOF_USER_TABLE,
+    array('rename' => array(
+        'user_password' => array(
+            'name'=>'user_password_hash'
+    ))));
+
+// not working for postgres, postgres doesn't know concat
 fof_db_query("update $FOF_USER_TABLE set user_password_hash = md5(concat(user_password_hash, user_name))");
 
 print "Done.<hr>";
@@ -290,14 +346,19 @@
 
 
 <?php
-$result = fof_db_query("show columns from $FOF_FEED_TABLE like 'feed_cache_attempt_date'");
 
-if(mysql_num_rows($result) == 0)
+if (!isFieldPresent("$FOF_FEED_TABLE", 'feed_cache_attempt_date'))
 {
 
 print "Upgrading schema...";
 
-fof_db_query("ALTER TABLE $FOF_FEED_TABLE ADD `feed_cache_attempt_date` INT( 11 ) DEFAULT '0' AFTER `feed_cache_date` ;");
+alterTable($FOF_FEED_TABLE, 
+    array('add' => array( 
+        'feed_cache_attempt_date' => array(
+            'type' => 'integer',
+            'length' => 11,
+            'default' => '0'
+        ))));
 
 print "Done.<hr>";
 }
@@ -340,7 +401,7 @@
 
 <?php
 	$result = fof_db_query("select * from $FOF_USER_TABLE where user_name = 'admin'");
-	if(mysql_num_rows($result) == 0) {
+    if($result->numRows() == 0) {
 ?>
 
 You now need to choose an initial password for the 'admin' account:<br>
