Connect to a database cluster

Database Service in Google Distributed Cloud (GDC) air-gapped lets you connect client tools to your database cluster to query data and manage workloads. You can retrieve connection details and the certificate authority (CA) certificate from the GDC console or by using the Kubernetes API.

By default, a database cluster allows connections only from within the same Kubernetes cluster and project. To allow connections from another project, see Enable cross-project connections. To connect from IP addresses outside your GDC organization, see Enable external connections.

Before you begin

To connect to a database cluster, request the necessary Identity and Access Management (IAM) permissions from your administrator.

Request IAM roles

To retrieve connection details for a database cluster, contact your IAM Project Admin to request the Database Project Admin (project-db-admin) role in your project namespace.

Connect to a database cluster

The following steps use psql as an example. Exact steps vary depending on your client software.

Console

  1. Sign in to the GDC console and go to the Connectivity section of the Database Service page for the database cluster. This page includes:

    • The password of the administrator account (the username is dbsadmin)
    • Hostname and port number of the database cluster's primary endpoint
    • If the database cluster allows external connection from outside of the organization.
    • A psql command for connecting to the cluster
    • A link to download the certificate authority (CA) certificate of the database cluster
  2. Download the CA certificate from the GDC console in the Connectivity section of the Database Service page for your database cluster.

  3. Configure your client to use the CA certificate to verify the database. For psql clients, set the PGSSLROOTCERT env variable to the path of the certificate file and the PGSSLMODE env variable to your preference:

    export PGSSLROOTCERT=path/to/accounts_cert.pem
    export PGSSLMODE="verify-full"
    
  4. Connect to the database from your client software. If you're using psql, run the following command:

    PGPASSWORD=DB_PASSWORD psql -h DB_HOSTNAME -p PORT -U USERNAME -d postgres
    

Replace the following variables:

  • path/to/: the path to the accounts_cert.pem certificate.
  • DB_PASSWORD: the password from the console.
  • DB_HOSTNAME: the database hostname from the console.
  • DB_PORT: the database port number from the console.
  • DB_USERNAME: the database username from the console.

API

  1. Retrieve the database endpoint from the database cluster status:

    kubectl get dbcluster.postgresql.dbadmin.gdc.goog DBCLUSTER_NAME -n USER_PROJECT -o=jsonpath='{.status.primary.url}'
    
  2. Download the CA certificate from the Kubernetes secret:

    kubectl get secret dbs-certificates -n USER_PROJECT -o json | jq -r '.data."dbs-pg-cert-DBCLUSTER_NAME"' | base64 -d > path/to/ca.crt
    
  3. Configure your client to use the CA certificate to verify the database. For psql clients, you can set the PGSSLROOTCERT env variable to the path of the certificate file and the PGSSLMODE env variable to your preference:

    export PGSSLROOTCERT=path/to/ca.crt
    export PGSSLMODE="verify-full"
    
  4. Connect to the database from your client software. If you're using psql, run the following command:

    PGPASSWORD=DB_PASSWORD psql -h DB_HOSTNAME -p DB_PORT -U DB_USERNAME -d postgres
    

Replace the following variables:

  • USER_PROJECT: the name of the user project where the database cluster was created.
  • DBCLUSTER_NAME: the name of the database cluster.
  • path/to/: the path to the database CA certificate.
  • DB_PASSWORD: database password for administrator user.
  • DB_HOSTNAME: the hostname from the database cluster status.
  • DB_PORT: the database port number from the database cluster status.
  • DB_USERNAME: with the database username (default is dbsadmin).