Connect to CrateDB

Note

This page documents the CrateDB Database API client.

For help using the SQLAlchemy dialect, consult the SQLAlchemy dialect documentation.

See also

Supplementary information about the CrateDB Database API client can be found in the data types appendix and the compatibility notes.

For general help using the Database API, consult PEP 0249.

Table of Contents

Connect to a Single Node

To connect to a single CrateDB node, use the connect() function, like so:

>>> connection = client.connect("<NODE_URL>", username="<USERNAME>")

Here, replace <NODE_URL> with a URL pointing to the HTTP endpoint of a CrateDB node. Replace <USERNAME> with the username you are authenticating as.

Note

This example authenticates as crate, the default database user in CrateDB versions 2.1.x and later. This might not work for you.

Consult the Authentication section for more information.

Example node URLs:

  • http://localhost:4200/
  • http://crate-1.vm.example.com:4200/
  • http://198.51.100.1:4200/

If the CrateDB hostname is crate-1.vm.example.com and CrateDB is listening for HTTP requests on port 4200, the node URL would be http://crate-1.vm.example.com:4200/.

Tip

If a <NODE_URL> argument is not provided, the library will attempt to connect to CrateDB on the local host with the default HTTP port number, i.e. http://localhost:4200/.

So, if you’re just getting started with CrateDB, the first time you connect, you can probably omit this argument.

Connect to Multiple Nodes

To connect to one of multiple nodes, pass a list of database URLs to the connect() function, like so:

>>> connection = client.connect(["<NODE_1_URL>", "<NODE_2_URL>"], ...)

Here, <NODE_1_URL> and <NODE_2_URL> correspond to two node URLs, as described in the previous section.

You can pass in as many node URLs as you like.

Tip

For every query, the client will attempt to connect to each node in sequence until a successful connection is made. Nodes are moved to the end of the list each time they are tried.

Over multiple query executions, this behaviour functions as client-side round-robin load balancing. (This is analogous to round-robin DNS.)

Connection Options

HTTPS

You can connect to a CrateDB client via HTTPS by specifying https in the URL:

>>> connection = client.connect('https://localhost:4200/', ...)

Caution

By default, server certificates are not verified.

See also

The CrateDB reference has a section on setting up SSL. This will be useful background reading for the following two subsections.

Server Verification

You can enable server SSL certificate verification by passing in the verify_ssl_cert keyword argument and setting it to True.

However, in order to do so, you also need to specify the certificate file of the Certificate Authority (CA) used to sign the server SSL certificate. You can do this using the ca_cert keyword argument.

Here’s how you might do that:

>>> connection = client.connect(..., ca_cert="<CA_CERT_FILE>", verify_ssl_cert=True)

Here, replace <CA_CERT_FILE> with the path to the CA certificate file.

Client Verification

The client also supports client verification via client certificates.

Here’s how you might do that:

>>> connection = client.connect(..., cert_file="<CERT_FILE>", key_file="<KEY_FILE>")

Here, replace <CERT_FILE> with the path to the client certificate file, and <KEY_FILE> with the path to the client private key file.

Tip

Often, you will want to perform server verification and client verification. In such circumstances, you can combine the two methods above to do both at once.

Timeout

Connection timeouts (in seconds) can be configured with the optional timeout argument:

>>> connection = client.connect(..., timeout=5)

Here, replace ... with the rest of your arguments.

Note

If no timeout is specified, the client will use the default Python socket timeout.

Tracebacks

Tracebacks in the event of a connection error will be printed if you set the optional error_trace argument to True, like so:

>>> connection = client.connect(..., error_trace=True)

Authentication

Note

Authentication was introduced in CrateDB versions 2.1.x.

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

See the compatibility notes for more information.

You can authenticate with CrateDB like so:

>>> connection = client.connect(..., username="<USERNAME>", password="<PASSWORD>")

Here, replace <USERNAME> and <PASSWORD> with the appropriate username and password.

Tip

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.

Schema Selection

You can select a schema using the optional schema argument, like so:

>>> connection = client.connect(..., schema="<SCHEMA>")

Here, replace <SCHEMA> with the name of your schema, and replace ... with the rest of your arguments.

Tip

The default CrateDB schema is doc, and if you do not specify a schema, this is what will be used.

However, you can query any schema you like by specifying it in the query.

Next Steps

Once you’re connected, you can query CrateDB.

See also

Check out the sample application (and the corresponding documentation) for a practical demonstration of this driver in use.