Skip to main content
Client applications, including , work by establishing a network connection to a CockroachDB cluster. The client connection parameters determine which CockroachDB cluster they connect to, and how to establish this network connection.

Supported connection parameters

Most client apps, including cockroach client commands, determine which CockroachDB server to connect to using a PostgreSQL connection URL. When using a URL, a client can also specify additional SQL-level parameters. This mode provides the most configuration flexibility. In addition, all cockroach client commands also accept discrete connection parameters that can specify the connection parameters separately from a URL.

When to use a URL and when to use discrete parameters

Specifying client parameters using a URL may be more convenient during experimentation, as it facilitates copy-pasting the connection parameters (the URL) between different tools: the output of cockroach start, other cockroach commands, GUI database visualizer, programming tools, etc. Discrete parameters may be more convenient in automation, where the components of the configuration are filled in separately from different variables in a script or a service manager.

Connect using a URL

A connection URL has the following format:
cockroach client commands also support UNIX domain socket URIs of the following form:
For cockroach commands that accept a URL, you can specify the URL with the command-line flag --url. If --url is not specified but the environment variable COCKROACH_URL is defined, the environment variable is used. Otherwise, the cockroach command will use discrete connection parameters as described below.
The <database part is not used for other than . A warning is currently printed if it is mistakenly specified, and future versions of CockroachDB may return an error in that case.

Additional connection parameters

The following additional parameters can be passed after the ? character in the URL. After the first parameter is specified, any additional parameters must be separated by an ampersand (&).

Supported options parameters

CockroachDB supports the following options parameters. After the first options parameter is specified, any additional parameters in the same connection string must be separated by a space.
Note that some drivers require certain characters to be properly encoded in URL connection strings. For example, spaces in a JDBC connection string must be specified as %20.

Secure connections with URLs

The following values are supported for sslmode, although only the first and the last are recommended for use.
Some client drivers and the cockroach commands do not support sslmode=allow and sslmode=prefer. Check the documentation of your SQL driver to determine whether these options are supported.

Convert a URL for different drivers

The subcommand cockroach convert-url converts a connection URL, such as those printed out by or included in the online documentation, to the syntax recognized by various . For example:

Example URL for an insecure connection

The following URL is suitable to connect to a CockroachDB node using an insecure connection:
This specifies a connection for the root user to server servername on port 26257 (the default CockroachDB SQL port), with mydb set as current database. sslmode=disable makes the connection insecure.

Example URL for a secure connection

The following URL is suitable to connect to a CockroachDB node using a secure connection:
This uses the following components:
  • User root
  • Host name servername, port number 26257 (the default CockroachDB SQL port)
  • Current database mydb
  • SSL/TLS mode verify-full:
    • Root CA certificate path/to/ca.crt
    • Client certificate path/to/client.username.crt
    • Client key path/to/client.username.key
For details about how to create and manage SSL/TLS certificates, see and .

Example URI for a Unix domain socket

The following URI is suitable to connect to a CockroachDB cluster listening for Unix domain socket connections at /path/to/client:
This specifies a connection for the root user to an insecure cluster listening for a socket connection (e.g., a cluster started with the ) at /path/to/client, and on port 26257.

Example URI for connecting to a database with a user-defined schema

The following URI connects to a CockroachDB cluster with a user-defined schema named max_schema in the movr database using the options parameter.
The options=-c search_path=max_schema parameter is URL-encoded in the example above.

Connect using discrete parameters

Most accept connection parameters as separate, discrete command-line flags, in addition (or in replacement) to --url which specifies all parameters as a URL. For each command-line flag that directs a connection parameter, CockroachDB also recognizes an environment variable. The environment variable is used when the command-line flag is not specified.

Example command-line flags for an insecure connection

The following command-line flags establish an insecure connection:
This specifies a connection for the root user to server servername on port 26257 (the default CockroachDB SQL port). --insecure makes the connection insecure.

Example command-line flags for a secure connection

The following command-line flags establish a secure connection:
This uses the following components:
  • User root
  • Host name servername, port number 26257 (the default CockroachDB SQL port)
  • SSL/TLS enabled, with settings:
    • Root CA certificate path/to/certs/ca.crt
    • Client certificate path/to/client.<user.crt (path/to/certs/client.root.crt with --user root)
    • Client key path/to/client.<user.key (path/to/certs/client.root.key with --user root)
When using discrete connection parameters, the file names of the CA and client certificates and client key are derived automatically from the value of --certs-dir.

Using both URL and client parameters

Most cockroach commands accept both a URL and client parameters. The information contained therein is combined in the order it appears in the command line. This combination is useful so that discrete command-line flags can override settings not otherwise set in the URL.

Example override of the current database

The cockroach start command prints out the following connection URL, which connects to the defaultdb database:
To specify mydb as the current database using , run the following command:
This is equivalent to:

See also