Connect to CrateDB

Table of contents

Authentication

Note

These examples authenticate as crate, the default database user in CrateDB versions 2.1.x and later.

If you are using CrateDB 2.1.x or later, you must supply a username. If you are using earlier versions of CrateDB, this parameter is not supported.

See the compatibility notes for more information.

If you have not configured a custom database user, you probably want to authenticate as the CrateDB superuser, which is crate. The superuser does not have a password, so you can omit the password argument.

DBAL

If you plan to query CrateDB via DBAL, you can get a connection from the DriverManager class using standard DBAL parameters, like so:

$params = array(
    'driverClass' => 'Crate\DBAL\Driver\PDOCrate\Driver',
    'user' => 'crate',
    'host' => 'localhost',
    'port' => 4200
);
$connection = \Doctrine\DBAL\DriverManager::getConnection($params);
$schemaManager = $connection->getSchemaManager();

With these connection parameters, the DriverManager will attempt to authenticate as crate with a CrateDB node listening on localhost:4200.

See also

For more help using DBAL, consult the DBAL documentation.

Doctrine ORM

If you want to use the Object-Relational Mapping (ORM) features of Doctrine, you must set up an EntityManager instance.

Here’s a slightly modified version of the Doctrine provided example:

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array("/path/to/entity-files");
$isDevMode = false;

// the connection configuration
$params = array(
    'driverClass' => 'Crate\DBAL\Driver\PDOCrate\Driver',
    'user' => 'crate'
    'host' => 'localhost',
    'port' => 4200
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($params, $config);

Here’s what we changed in the above example:

  • Specified the CrateDB driver class

  • Specified the crate user

  • Configured the connection for localhost:4200

See also

The CrateDB DBAL driver provides three custom type objects. Consult the data types appendix for more information about type maps and column type definitions.

Next steps

Use the standard the DBAL documentation or Doctrine ORM documentation for the rest of your setup process.