# Connect to the Database

This page has instructions for connecting to a CockroachDB cluster from your application using various programming languages. Each example shows a connection string for a secure local cluster to a `bank` database by a user named `maxroach`. Depending on your cluster's configuration, you may need to edit this connection string.

For a reference that lists all of the supported cluster connection parameters, see Connection Parameters.

## Before you begin

Make sure you have already:

- Set up a local cluster.
- Installed a Postgres client.

## Connect

    $ cockroach sql --certs-dir=certs --host=localhost:26257

For more information about how to use the built-in SQL client, see the `cockroach sql` reference docs.

    import (
        "database/sql"
        "fmt"
        "log"
        _ "github.com/lib/pq"
    )

    db, err := sql.Open("postgres",
            "postgresql://maxroach@localhost:26257/bank?ssl=true&sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.maxroach.key&sslcert=certs/client.maxroach.crt")
    if err != nil {
        log.Fatal("error connecting to the database: ", err)
    }
    defer db.Close()

For complete examples, see:

- Build a Go App with CockroachDB (pgx)
- Build a Go App with CockroachDB and GORM

    import java.sql.*;
    import javax.sql.DataSource;

    PGSimpleDataSource ds = new PGSimpleDataSource();
    ds.setServerName("localhost");
    ds.setPortNumber(26257);
    ds.setDatabaseName("bank");
    ds.setUser("maxroach");
    ds.setPassword(null);
    ds.setSsl(true);
    ds.setSslMode("require");
    ds.setSslCert("certs/client.maxroach.crt");
    ds.setSslKey("certs/client.maxroach.key.pk8");
    ds.setReWriteBatchedInserts(true); // add `rewriteBatchedInserts=true` to pg connection string
    ds.setApplicationName("BasicExample");

For complete examples, see:

- Build a Java App with CockroachDB (JDBC)
- Build a Java App with CockroachDB and Hibernate

    import psycopg2

    conn = psycopg2.connect(
        database='bank',
        user='maxroach',
        sslmode='require',
        sslrootcert='certs/ca.crt',
        sslkey='certs/client.maxroach.key',
        sslcert='certs/client.maxroach.crt',
        port=26257,
        host='localhost'
    )

For complete examples, see:

- Build a Python App with CockroachDB (psycopg2)
- Build a Python App with CockroachDB and SQLAlchemy
- Build a Python App with CockroachDB and Django

## See also

Reference information related to this task:

- Connection parameters
- Manual deployments
- Orchestrated deployments
- Start a local cluster (secure)

Other common tasks:

- Insert Data
- Query Data
- Update Data
- Delete Data
- Run Multi-Statement Transactions
- Error Handling and Troubleshooting
- Optimize Query Performance
- Hello World Example apps