diff --git includes/database/database.inc includes/database/database.inc
index b384afe..a5b2865 100644
--- includes/database/database.inc
+++ includes/database/database.inc
@@ -289,6 +289,11 @@ abstract class DatabaseConnection extends PDO {
    */
   protected $prefixes = array();
 
+  /**
+   * The maximum length a table, column, alias or index name can be.
+   */
+  protected $max_indentifier_length = 64;
+
   function __construct($dsn, $username, $password, $driver_options = array()) {
     // Initialize and prepare the connection prefix.
     $this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : '');
diff --git includes/database/mysql/schema.inc includes/database/mysql/schema.inc
index 01933da..33d63ec 100644
--- includes/database/mysql/schema.inc
+++ includes/database/mysql/schema.inc
@@ -77,6 +77,7 @@ class DatabaseSchema_mysql extends DatabaseSchema {
    *   An array of SQL statements to create the table.
    */
   protected function createTableSql($name, $table) {
+    $this->validateIdentifier($name);
     $info = $this->connection->getConnectionOptions();
 
     // Provide defaults if needed.
@@ -130,6 +131,7 @@ class DatabaseSchema_mysql extends DatabaseSchema {
    *   The field specification, as per the schema data structure format.
    */
   protected function createFieldSql($name, $spec) {
+    $this->validateIdentifier($name);
     $sql = "`" . $name . "` " . $spec['mysql_type'];
 
     if (in_array($spec['mysql_type'], array('VARCHAR', 'CHAR', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'TEXT')) && isset($spec['length'])) {
@@ -262,6 +264,7 @@ class DatabaseSchema_mysql extends DatabaseSchema {
     }
     if (!empty($spec['indexes'])) {
       foreach ($spec['indexes'] as $index => $fields) {
+        $this->validateIdentifier($index);
         $keys[] = 'INDEX `' . $index . '` (' . $this->createKeysSqlHelper($fields) . ')';
       }
     }
@@ -296,6 +299,7 @@ class DatabaseSchema_mysql extends DatabaseSchema {
   }
 
   public function renameTable($table, $new_name) {
+    $this->validateIdentifier($new_name);
     if (!$this->tableExists($table)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot rename %table to %table_new: table %table doesn't exist.", array('%table' => $table, '%table_new' => $new_name)));
     }
@@ -317,6 +321,7 @@ class DatabaseSchema_mysql extends DatabaseSchema {
   }
 
   public function addField($table, $field, $spec, $keys_new = array()) {
+    $this->validateIdentifier($field);
     if (!$this->tableExists($table)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot add field %table.%field: table doesn't exist.", array('%field' => $field, '%table' => $table)));
     }
@@ -426,6 +431,7 @@ class DatabaseSchema_mysql extends DatabaseSchema {
   }
 
   public function addIndex($table, $name, $fields) {
+    $this->validateIdentifier($name);
     if (!$this->tableExists($table)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot add index %name to table %table: table doesn't exist.", array('%table' => $table, '%name' => $name)));
     }
diff --git includes/database/pgsql/database.inc includes/database/pgsql/database.inc
index d38b64a..5a4fa7c 100644
--- includes/database/pgsql/database.inc
+++ includes/database/pgsql/database.inc
@@ -55,6 +55,10 @@ class DatabaseConnection_pgsql extends DatabaseConnection {
 
     // Force PostgreSQL to use the UTF-8 character set by default.
     $this->exec("SET NAMES 'UTF8'");
+
+    // Retrieve the max identifier length. This is usually 63 characters
+    // but can be altered before PostreSQL is compiled.
+    $this->max_identifier_length = $this->query("SHOW max_identifier_length")->fetchField();
   }
 
   public function query($query, array $args = array(), $options = array()) {
diff --git includes/database/pgsql/schema.inc includes/database/pgsql/schema.inc
index 30f12ef..3675263 100644
--- includes/database/pgsql/schema.inc
+++ includes/database/pgsql/schema.inc
@@ -118,6 +118,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
    *   An array of SQL statements to create the table.
    */
   protected function createTableSql($name, $table) {
+    $this->validateIdentifier($name);
     $sql_fields = array();
     foreach ($table['fields'] as $field_name => $field) {
       $sql_fields[] = $this->createFieldSql($field_name, $this->processField($field));
@@ -176,6 +177,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
    *    The field specification, as per the schema data structure format.
    */
   protected function createFieldSql($name, $spec) {
+    $this->validateIdentifier($name);
     $sql = $name . ' ' . $spec['pgsql_type'];
 
     if (isset($spec['type']) && $spec['type'] == 'serial') {
@@ -314,6 +316,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
   }
 
   function renameTable($table, $new_name) {
+    $this->validateIdentifier($new_name);
     if (!$this->tableExists($table)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot rename %table to %table_new: table %table doesn't exist.", array('%table' => $table, '%table_new' => $new_name)));
     }
@@ -417,7 +420,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
 
   public function indexExists($table, $name) {
     // Details http://www.postgresql.org/docs/8.3/interactive/view-pg-indexes.html
-    $index_name = '{' . $table . '}_' . $name . '_idx';
+    $index_name = $this->validateIdentifier('{' . $table . '}_' . $name . '_idx');
     return (bool) $this->connection->query("SELECT 1 FROM pg_indexes WHERE indexname = '$index_name'")->fetchField();
   }
 
@@ -495,6 +498,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
   }
 
   public function changeField($table, $field, $field_new, $spec, $new_keys = array()) {
+    $this->validateIdentifier($field_new);
     if (!$this->fieldExists($table, $field)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot change the definition of field %table.%name: field doesn't exist.", array('%table' => $table, '%name' => $field)));
     }
diff --git includes/database/schema.inc includes/database/schema.inc
index e57c94b..8771b7d 100644
--- includes/database/schema.inc
+++ includes/database/schema.inc
@@ -230,7 +230,7 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
     $args = func_get_args();
     $info = $this->getPrefixInfo($table);
     $args[0] = $info['table'];
-    return implode('_', $args);
+    return $this->validateIdentifier(implode('_', $args));
   }
 
   /**
@@ -674,6 +674,16 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
   public function prepareComment($comment, $length = NULL) {
     return $this->connection->quote($comment);
   }
+
+  /**
+   * Validate an identifier.
+   */
+  public function validateIdentifier($identifier) {
+    if (strlen($identifier) > $this->connection->max_identifier_length) {
+      throw new DatabaseSchemaIdentifierLengthException("Identifier length exceeds maximum length of " . $this->connection->max_identifier_length . ': ' . $identifier);
+    }
+    return $identifier;
+  }
 }
 
 /**
@@ -695,6 +705,14 @@ class DatabaseSchemaObjectExistsException extends Exception {}
 class DatabaseSchemaObjectDoesNotExistException extends Exception {}
 
 /**
+ * Exception thrown if an identifier is not valid.
+ *
+ * For example this Exception is thrown when an identifier has a greater length
+ * than the maximum length the database will allow.
+ */
+class DatabaseSchemaIdentifierLengthException extends Exception {}
+
+/**
  * @} End of "defgroup schemaapi".
  */
 
diff --git includes/database/sqlite/schema.inc includes/database/sqlite/schema.inc
index 11ecd88..7171f24 100644
--- includes/database/sqlite/schema.inc
+++ includes/database/sqlite/schema.inc
@@ -42,6 +42,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
    *   An array of SQL statements to create the table.
    */
   public function createTableSql($name, $table) {
+    $this->validateIdentifier($name);
     $sql = array();
     $sql[] = "CREATE TABLE {" . $name . "} (\n" . $this->createColumsSql($name, $table) . "\n);\n";
     return array_merge($sql, $this->createIndexSql($name, $table));
@@ -60,6 +61,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
     }
     if (!empty($schema['indexes'])) {
       foreach ($schema['indexes'] as $key => $fields) {
+        $this->validateIdentifier($info['table'] . '_' . $key);
         $sql[] = 'CREATE INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $key . ' ON ' . $info['table'] . ' (' . $this->createKeySql($fields) . "); \n";
       }
     }
@@ -103,7 +105,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
         $return[] = $field;
       }
     }
-    return implode(', ', $return);
+    return $this->validateIdentifier(implode(', ', $return));
   }
 
   /**
@@ -146,6 +148,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
    *    The field specification, as per the schema data structure format.
    */
   protected function createFieldSql($name, $spec) {
+    $this->validateIdentifier($name);
     if (!empty($spec['auto_increment'])) {
       $sql = $name . " INTEGER PRIMARY KEY AUTOINCREMENT";
       if (!empty($spec['unsigned'])) {
@@ -231,6 +234,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
   }
 
   public function renameTable($table, $new_name) {
+    $this->validateIdentifier($new_name);
     if (!$this->tableExists($table)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot rename %table to %table_new: table %table doesn't exist.", array('%table' => $table, '%table_new' => $new_name)));
     }
@@ -277,6 +281,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
   }
 
   public function addField($table, $field, $specification, $keys_new = array()) {
+    $this->validateIdentifier($field);
     if (!$this->tableExists($table)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot add field %table.%field: table doesn't exist.", array('%field' => $field, '%table' => $table)));
     }
@@ -493,6 +498,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
   }
 
   public function changeField($table, $field, $field_new, $spec, $keys_new = array()) {
+    $this->validateIdentifier($field_new);
     if (!$this->fieldExists($table, $field)) {
       throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot change the definition of field %table.%name: field doesn't exist.", array('%table' => $table, '%name' => $field)));
     }
diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php
index fa9917d..51e5db2 100644
--- modules/simpletest/drupal_web_test_case.php
+++ modules/simpletest/drupal_web_test_case.php
@@ -664,7 +664,7 @@ class DrupalUnitTestCase extends DrupalTestCase {
     drupal_static_reset();
 
     // Generate temporary prefixed database to ensure that tests have a clean starting point.
-    $this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
+    $this->databasePrefix = Database::getConnection()->prefixTables('{st' . mt_rand(1000, 1000000) . '}');
 
     // Create test directory.
     $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
