diff --git includes/database/pgsql/query.inc includes/database/pgsql/query.inc
index 8825229..8179e60 100644
--- includes/database/pgsql/query.inc
+++ includes/database/pgsql/query.inc
@@ -67,7 +67,31 @@ class InsertQuery_pgsql extends InsertQuery {
     elseif ($options['return'] == Database::RETURN_INSERT_ID) {
       $options['return'] = Database::RETURN_NULL;
     }
-    $last_insert_id = $this->connection->query($stmt, array(), $options);
+
+    try {
+      $last_insert_id = $this->connection->query($stmt, array(), $options);
+    } catch(PDOException $e) {
+      if ($e->getCode() == 55000) {
+        // The query can fail if a value was inserted into the serial column and
+        // nextval() was never called. In that case, the last_insert_id is known
+        // by the caller and NULL is returned.
+        $last_insert_id = NULL;
+      }
+      else {
+        // If it is a different exception, re-throw it.
+        throw $e;
+      }
+    }
+
+    // If a value was inserted into a serial column, update the sequence.
+    if (!empty($table_information->serial_fields)) {
+      foreach ($table_information->serial_fields as $index => $serial_field) {
+        if (in_array($serial_field, $this->insertFields)) {
+          $curval = $this->connection->query("SELECT MAX(" . $serial_field . ") FROM {" . $this->table . "}")->fetchField();
+          $this->connection->query("SELECT SETVAL('" . $table_information->sequences[$index] . "', :curval)", array(':curval' => $curval));
+        }
+      }
+    }
 
     // Re-initialize the values array so that we can re-use this query.
     $this->insertValues = array();
