Back to top

ReQL command: connect

Command syntax

r.connect(options) → connection

Description

Create a new connection to the database server. The keyword arguments are:

  • host: host of the RethinkDB instance. The default value is localhost.
  • port: the driver port, by default 28015.
  • db: the database used if not explicitly specified in a query, by default test.
  • user: the user account to connect as (default admin).
  • password: the password for the user account to connect as (default '', empty).
  • timeout: timeout period in seconds for the connection to be opened (default 20).
  • ssl: a hash of options to support SSL connections (default None). Currently, there is only one option available, and if the ssl option is specified, this key is required:
    • ca_certs: a path to the SSL CA certificate.

If the connection cannot be established, a ReqlDriverError exception will be thrown.

The returned connection object will have two methods on it returning the connection’s port and address:

conn.client_port()
conn.client_address()

Using SSL with RethinkDB requires proxy software on the server, such as Nginx, HAProxy or an SSL tunnel. RethinkDB will encrypt traffic and verify the CA certification to prevent man-in-the-middle attacks. Consult your proxy’s documentation for more details.

Alternatively, you may use RethinkDB’s built-in TLS support.

The RethinkDB Python driver includes support for asynchronous connections using Tornado and Twisted. Read the asynchronous connections documentation for more information.

Note: Currently, the Python driver is not thread-safe. Each thread or multiprocessing PID should be given its own connection object. (This is likely to change in a future release of RethinkDB; you can track issue #2427 for progress.)

Example: Open a connection using the default host and port, specifying the default database.

conn = r.connect(db='marvel')

Example: Open a new connection to the database.

conn = r.connect(host='localhost',
                 port=28015,
                 db='heroes')

Example: Open a new connection to the database, specifying a user/password combination for authentication.

conn = r.connect(host='localhost',
                 port=28015,
                 db='heroes',
                 user='herofinder',
                 password='metropolis')

Example: Open a new connection to the database using an SSL proxy.

conn = r.connect(host='localhost',
                 port=28015,
                 ssl={'ca_certs': '/path/to/ca.crt'})

Example: Use a with statement to open a connection and pass it to a block. Using this style, the connection will be automatically closed when execution reaches the end of the block.

with r.connect(db='marvel') as conn:
    r.table('superheroes').run(conn)

Get more help

Couldn't find what you were looking for?