{"token_count": 6663}

# Database Access with AlloyDB

Teleport can provide secure access to AlloyDB via the [Teleport Database Service](https://goteleport.com/docs/enroll-resources/database-access.md). This allows for fine-grained access control through [Teleport's RBAC](https://goteleport.com/docs/enroll-resources/database-access/rbac.md).

In this guide, you will:

1. Configure your AlloyDB database with a service account.
2. Add the database to your Teleport cluster.
3. Connect to the database via Teleport.

## How it works

The Teleport Database Service uses IAM authentication to communicate with AlloyDB. When a user connects to the database via Teleport, the Teleport Database Service obtains Google Cloud credentials and authenticates to Google Cloud as an IAM principal with permissions to access the database.

![Teleport Architecture for AlloyDB Access](/docs/assets/images/architecture-4d2cc43d241a8934656315deb2bb278b.png)

## Prerequisites

- A running Teleport cluster accessible at a hostname with a valid TLS certificate. If you want to get started with Teleport, [sign up](https://goteleport.com/signup) for a free trial or [set up a demo environment](https://goteleport.com/docs/get-started/deploy-community.md).

- The `tctl` and `tsh` clients.

  Installing `tctl` and `tsh` clients

  1. Determine the version of your Teleport cluster. The `tctl` and `tsh` clients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at `/v1/webapi/find` and use a JSON query tool to obtain your cluster version. Replace teleport.example.com:443 with the web address of your Teleport Proxy Service:

     **Mac/Linux**

     ```
     $ TELEPORT_DOMAIN=teleport.example.com:443
     $ TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')"
     ```

     **Windows - Powershell**

     ```
     $ $TELEPORT_DOMAIN = "teleport.example.com:443"
     $ $TELEPORT_VERSION = (Invoke-RestMethod -Uri "https://${TELEPORT_DOMAIN}/v1/webapi/find").server_version
     ```

  2. Follow the instructions for your platform to install `tctl` and `tsh` clients:

     **Mac**

     Download the signed macOS .pkg installer for Teleport, which includes the `tctl` and `tsh` clients:

     ```
     $ curl -O https://cdn.teleport.dev/teleport-${TELEPORT_VERSION?}.pkg
     ```

     In Finder double-click the `pkg` file to begin installation.

     ---

     DANGER

     Using Homebrew to install Teleport is not supported. The Teleport package in Homebrew is not maintained by Teleport and we can't guarantee its reliability or security.

     ---

     **Windows - Powershell**

     ```
     $ curl.exe -O https://cdn.teleport.dev/teleport-v$TELEPORT_VERSION-windows-amd64-bin.zip
     Unzip the archive and move the `tctl` and `tsh` clients to your %PATH%
     NOTE: Do not place the `tctl` and `tsh` clients in the System32 directory, as this can cause issues when using WinSCP.
     Use %SystemRoot% (C:\Windows) or %USERPROFILE% (C:\Users\<username>) instead.
     ```

     **Linux**

     All of the Teleport binaries in Linux installations include the `tctl` and `tsh` clients. For more options (including RPM/DEB packages and downloads for i386/ARM/ARM64) see our [installation page](https://goteleport.com/docs/installation/single-machine.md).

     ```
     $ curl -O https://cdn.teleport.dev/teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gz
     $ tar -xzf teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gz
     $ cd teleport
     $ sudo ./install
     Teleport binaries have been copied to /usr/local/bin
     ```

  Connecting with TLS routing disabled

  This guide's commands assume your Teleport cluster uses TLS routing (`proxy_listener_mode: multiplex`), where the `tctl` and `tsh` clients reach every Teleport service through the Proxy Service's web address on port `443`. If you're not sure whether this applies to your cluster, check with whoever manages it.

  If your cluster uses separate listener ports instead, adjust ports as follows:

  - **`tsh` commands** (e.g., `tsh login --proxy=...`): continue using the Proxy Service web address on port `3080` (or `443` if behind a load balancer). Do not change these to port `3025`.

  - **Direct `tctl` or Auth Service API commands**: use port `3025` for the Auth Service gRPC listener:

    ```
    $ tctl status --auth-server=teleport.example.com:3025
    ```

* Google Cloud account with an AlloyDB cluster and instance deployed, configured for [IAM database authentication](https://cloud.google.com/alloydb/docs/database-users/manage-iam-auth).

* `psql` installed and in your system `PATH`.

* A host (e.g., a Compute Engine instance) to run the Teleport Database Service.

* Check that you can connect to your Teleport cluster and verify that you can run `tctl` and `tsh` commands using your current credentials.

  1. Assign teleport.example.com to the domain name of the Teleport Proxy Service in your cluster and email\@example.com to your Teleport username.

  2. Authenticate to your Teleport cluster. This depends on whether your shell is interactive or not.

     **In an interactive shell:** Run the following command. By default, this triggers a multi-factor authentication prompt:

     ```
     $ tsh login --proxy=teleport.example.com --user=email@example.com
     $ tctl status
     Cluster  teleport.example.com
     Version  19.0.0-dev
     CA pin   sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678
     ```

     **On non-interactive environments:** If you are running `tsh` and `tctl` as an AI agent, in a CI/CD environment, or similar, make sure the `TELEPORT_IDENTITY_FILE` environment variable is assigned to a valid file path with credentials for your cluster. `tsh` and `tctl` read the file path from the environment variable and do not require a separate authentication step. If there is no identity file available, we recommend that you [set up Machine ID](https://goteleport.com/docs/machine-workload-identity/getting-started.md) to provision one automatically.

     When executing `tctl` commands with an identity file, you must pass the `--auth-server` flag to provide the Teleport Auth Service address, which is not included in the identity file. If you provide the Proxy Service address, `tctl` connects to the Proxy Service, which forwards traffic to and from the Teleport Auth Service. Update 443 to `3025` if you are contacting the Auth Service directly with `tctl`:

     ```
     $ tctl status --auth-server=teleport.example.com:443
     ```

     For `tsh` commands that read an identity file, you must pass the `--proxy` flag, which points `tsh` to the address of the Teleport Proxy Service:

     ```
     $ tsh status --proxy=teleport.example.com
     ```

     Ensure client commands can access your identity file. Replace path/to/identity/file with the path to your identity file:

     ```
     $ export TELEPORT_IDENTITY_FILE="${TELEPORT_IDENTITY_FILE:-path/to/identity/file}"
     ```

     Add the `--auth-server` or `--proxy` flags to all subsequent `tctl` and `tsh` commands.

  If you can connect to the cluster and run the `tctl status` command, you can use your current credentials to run subsequent `tctl` commands from your workstation. If you host your own Teleport cluster, you can also run `tctl` commands on the computer that hosts the Teleport Auth Service for full permissions.

## Step 1/4. Configure IAM and create a database user

In this step, you will create two required service accounts.

- `teleport-db-service`: used by the Teleport Database Service to access AlloyDB metadata and generate tokens.
- `alloydb-user`: used by end-users to authenticate to the database.

### Create a service account for the Teleport Database Service

**Google Cloud Console**

Go to [Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts) and create a service account named `teleport-db-service`. Assign the predefined [`roles/alloydb.client`](https://cloud.google.com/alloydb/docs/reference/iam-roles-permissions) role.

**gcloud CLI**

Set project-id to your GCP project ID.

```
1. Create the Service Account explicitly in the target project
$ gcloud iam service-accounts create teleport-db-service \
    --display-name="Teleport Database Service" \
    --project=project-id

2. Grant the role to that specific account
$ gcloud projects add-iam-policy-binding project-id \
    --member="serviceAccount:teleport-db-service@project-id.iam.gserviceaccount.com" \
    --role="roles/alloydb.client"
```

### Create the database user account

---

NOTE

If you already have a GCP service account for database access with the required roles, you can use it instead.

---

**Google Cloud Console**

Go to [Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts) and create a service account named `alloydb-user`. Assign these roles:

- `roles/alloydb.databaseUser`
- `roles/alloydb.client`
- [`roles/serviceusage.serviceUsageConsumer`](https://cloud.google.com/service-usage/docs/access-control#serviceusage.serviceUsageConsumer)

Then, on the `alloydb-user` overview page, go to the "Principals with Access" tab, click "Grant Access", and add `teleport-db-service` with the **Service Account Token Creator** role.

**gcloud CLI**

```
$ gcloud iam service-accounts create alloydb-user --display-name="AlloyDB User" --project=project-id

$ for role in roles/alloydb.databaseUser roles/alloydb.client roles/serviceusage.serviceUsageConsumer;
  do \
  gcloud projects add-iam-policy-binding project-id \
    --member="serviceAccount:alloydb-user@project-id.iam.gserviceaccount.com" \
    --role="$role"; \
  done \

$ gcloud iam service-accounts add-iam-policy-binding \
    alloydb-user@project-id.iam.gserviceaccount.com \
    --member="serviceAccount:teleport-db-service@project-id.iam.gserviceaccount.com" \
    --role="roles/iam.serviceAccountTokenCreator"
```

### Add the IAM database user to AlloyDB

---

NOTE

Skip this if your AlloyDB instance already has an IAM user for this service account.

---

Ensure [IAM authentication](https://cloud.google.com/alloydb/docs/database-users/manage-iam-auth) is enabled on your instance (the `alloydb.iam_authentication` flag must be set) before adding the User.

---

INSTANCE RESTART REQUIRED

Enabling the static `alloydb.iam_authentication` flag triggers a mandatory restart of the AlloyDB instance. This will cause a brief period of downtime (typically 60 - 120 seconds) as the instance performs a maintenance update. We recommend performing this during a scheduled maintenance window.

---

**Google Cloud Console**

1. Go to the AlloyDB Clusters page.
2. Click Edit on your primary-instance, scroll to Advanced configuration options, and under Flags ensure `alloydb.iam_authentication` is present and set to on.
3. Go to the Users page of your AlloyDB instance.
4. Click Add User Account.
5. Choose Cloud IAM authentication.
6. In the Principal field, enter `alloydb-user@project-id.iam`.

**gcloud CLI**

Enable IAM authentication on your instance:

```
$ gcloud alloydb instances update instance-name \
    --cluster=cluster-name \
    --region=region \
    --project=project-id \
    --database-flags=alloydb.iam_authentication=on
```

---

WARNING

If your instance already has custom database flags, include them in the `--database-flags` list along with `alloydb.iam_authentication=on`. Any flags you omit are reset to their default values.

To review the instance's current manually set flags:

```
$ gcloud alloydb instances describe instance-name \
    --cluster=cluster-name \
    --region=region
```

---

Create the IAM-based database user to link the service account to the database:

```
$ gcloud alloydb users create alloydb-user@project-id.iam \
    --cluster=cluster-name \
    --region=region \
    --project=project-id \
    --type=IAM_BASED
```

## Step 2/4. Create a Teleport Database Service host

The Teleport Database Service must run on a host that can reach the AlloyDB instance and authenticate with GCP.

---

NOTE

If you already have a host running the Teleport Database Service with the `teleport-db-service` credentials, skip to Step 3.

---

Create a GCE instance and attach the `teleport-db-service` service account in the "Identity and API access" section.

Attaching the service account to an existing GCE instance

**Google Cloud Console**

1. Navigate to [VM instances](https://console.cloud.google.com/compute/instances) and open your instance.
2. Stop the instance.
3. Edit the instance, find **Service account** under **Identity and API access**, and select `teleport-db-service`.
4. Save and restart.

**gcloud CLI**

If you have an existing GCE instance, you can attach the service account using the `gcloud` command-line tool. Set the variables:

- instance-name instance name
- zone instance zone
- project-id GCP project ID

```
The instance must be stopped before modifying the service account
1. Stop the instance
gcloud compute instances stop instance-name \
    --project=project-id \
    --zone=zone

2. Update the Service Account and Scopes
gcloud compute instances set-service-account instance-name \
    --service-account=teleport-db-service@project-id.iam.gserviceaccount.com \
    --scopes=https://www.googleapis.com/auth/cloud-platform \
    --project=project-id \
    --zone=zone

3. Restart the instance
gcloud compute instances start instance-name \
    --project=project-id \
    --zone=zone
```

Verify the instance is running with the correct service account and scopes:

```
$ gcloud compute instances describe instance-name --zone=zone \
   --format="yaml(status,serviceAccounts)"
```

If the Database Service is running outside of GCE, use [workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation) to provide credentials.

Using service account keys (not recommended for production)

Create a JSON key for the `teleport-db-service` account. If you use `systemd` to start Teleport, add the environment variable to the service's `EnvironmentFile`:

```
$ echo 'GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json' | \
sudo tee -a /etc/default/teleport
```

---

WARNING

Service account keys are a security risk. Use workload identity or attached service accounts in production. See [Google Cloud authentication docs](https://cloud.google.com/docs/authentication#service-accounts) for details.

---

## Step 3/4. Configure Teleport

In this step, you will configure the Teleport Database Service to connect to AlloyDB and handle authentication and access on behalf of users.

### Install the Teleport Database Service

To install Teleport binaries on your Linux server, the recommended installation method is the cluster install script. This script is served by your Teleport cluster's Proxy Service and automatically selects the correct version, edition, and installation mode to match your cluster.

1. Remove any existing Teleport binaries on your system:

   ```
   $ sudo rm -f /usr/local/bin/{tsh,teleport,tctl,tbot,fdpass-teleport,teleport-update}
   ```

2. Assign teleport.example.com:443 to your Teleport cluster hostname and port, but not the scheme (https\://).

3. Run your cluster's install script:

   ```
   $ curl "https://teleport.example.com:443/scripts/install.sh" | sudo bash
   ```

### Create a join token

The Database Service requires a valid join token to join your Teleport cluster. Run the following `tctl` command and save the token output in `/tmp/token` on the server that will run the Database Service:

```
$ tctl tokens add --type=db --format=text
abcd123-insecure-do-not-use-this
```

### Configure and start the Database Service

In the command below, replace teleport.example.com:443 with the host and port of your Teleport Proxy Service or Enterprise Cloud site, and replace connection-uri with your AlloyDB connection URI.

The connection URI has the format `projects/PROJECT-ID/locations/REGION/clusters/CLUSTER/instances/INSTANCE`. You can copy it from the AlloyDB instance details page in the Google Cloud console.

![AlloyDB Connection URI](/docs/assets/images/connection-uri-bd28375d3047632a4c28248c6cdc8b07.png)

Run the command as follows. Make sure to include the mandatory `alloydb://` prefix in the specified URI.

```
$ sudo teleport db configure create \
   -o file \
   --name=alloydb \
   --protocol=postgres \
   --labels=env=dev \
   --token=/tmp/token \
   --proxy=teleport.example.com:443  \
   --uri=alloydb://connection-uri
```

By default, Teleport uses the private AlloyDB endpoint. To use a [public](https://cloud.google.com/alloydb/docs/connect-public-ip) or [Private Service Connect (PSC)](https://cloud.google.com/alloydb/docs/about-private-service-connect) endpoint instead, set `endpoint_type` in the config:

```
db_service:
  databases:
    - name: alloydb
      protocol: postgres
      uri: alloydb://projects/PROJECT-ID/locations/REGION/clusters/CLUSTER/instances/INSTANCE
      gcp:
        alloydb:
          endpoint_type: public  # private | public | psc

```

Create `alloydb.yaml`:

```
kind: db
version: v3
metadata:
  name: alloydb-dynamic
  labels:
    env: dev
spec:
  protocol: "postgres"
  uri: "alloydb://connection-uri"
  gcp:
    alloydb:
      endpoint_type: private

```

Apply it:

```
$ tctl create -f alloydb.yaml
```

Start the Database Service:

Start the Teleport Database Service. The instructions depend on how you installed the Teleport Database Service and whether your system supports systemd:

**Package Manager**

Configure the Teleport Database Service to start automatically when the host boots up by creating a systemd service for it. On the host where you will run the Teleport Database Service, enable and start Teleport:

```
$ sudo systemctl enable teleport
$ sudo systemctl start teleport
```

You can check the status of the Teleport Database Service with `systemctl status teleport` and view its logs with `journalctl -fu teleport`.

**TAR Archive**

Configure the Teleport Database Service to start automatically when the host boots up by creating a systemd service for it. On the host where you will run the Teleport Database Service, create a systemd service configuration for Teleport, enable the Teleport service, and start Teleport:

```
$ sudo teleport install systemd -o /etc/systemd/system/teleport.service
$ sudo systemctl enable teleport
$ sudo systemctl start teleport
```

You can check the status of the Teleport Database Service with `systemctl status teleport` and view its logs with `journalctl -fu teleport`.

**No systemd**

On the host where you will run the Teleport Database Service, start Teleport:

```
$ sudo teleport start --config=/etc/teleport.yaml
```

Teleport runs in the foreground and outputs logs for the services it is running.

## Step 4/4. Connect to your database

You will only be able to see databases that your Teleport role has access to. See our [RBAC guide](https://goteleport.com/docs/zero-trust-access/rbac-get-started.md) for more details.

---

TIP

To modify an existing user to provide access to the Database Service, see [Database Access Controls](https://goteleport.com/docs/enroll-resources/database-access/rbac.md)

---

**Teleport Community Edition**

Create a local Teleport user with the built-in `access` role:

```
$ tctl users add \
  --roles=access \
  --db-users="*" \
  --db-names="*" \
  alice
```

**Teleport Enterprise/Enterprise Cloud**

Create a local Teleport user with the built-in `access` and `requester` roles:

```
$ tctl users add \
  --roles=access,requester \
  --db-users="*" \
  --db-names="*" \
  alice
```

| Flag         | Description                                                                                                                              |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `--roles`    | List of roles to assign to the user. The builtin `access` role allows them to connect to any database server registered with Teleport.   |
| `--db-users` | List of database usernames the user will be allowed to use when connecting to the databases. A wildcard allows any user.                 |
| `--db-names` | List of logical databases (aka schemas) the user will be allowed to connect to within a database server. A wildcard allows any database. |

---

WARNING

Database names are only enforced for PostgreSQL, MongoDB, and Cloud Spanner databases.

---

For more detailed information about database access controls and how to restrict access see [RBAC](https://goteleport.com/docs/enroll-resources/database-access/rbac.md) documentation.

Log in and list databases:

```
$ tsh login --proxy=teleport.example.com --user=alice
$ tsh db ls
  Name    Description Labels
  ------- ----------- -------
  alloydb GCP AlloyDB env=dev
```

The database user name is shown on the Users page of your AlloyDB instance. Connect using the service account name (minus `.gserviceaccount.com`):

```
$ tsh db connect --db-user=alloydb-user@project-id.iam --db-name=postgres alloydb
```

---

TIP

To connect to the database using a tool of your choice, like a graphical database client, you can create a local tunnel and point it to your software:

```
$ tsh proxy db alloydb --tunnel --db-user=alloydb-user@project-id.iam --db-name=postgres
```

See [Database GUI Clients](https://goteleport.com/docs/connect-your-client/third-party/gui-clients.md) guide for more information on specific tools.

---

---

TIP

From version `17.1`, you can also [connect via the Web UI](https://goteleport.com/docs/connect-your-client/teleport-clients/web-ui.md#starting-a-database-session).

---

To log out:

```
$ tsh db logout alloydb
Or for all databases:
$ tsh db logout
```

## Optional: least-privilege access

When possible, enforce least-privilege by defining custom IAM roles that grant only the required permissions.

### Custom role for the Teleport Database Service

The Teleport Database Service, running as the `teleport-db-service` service account, needs permissions to access the AlloyDB instance.

Create a custom role with the following permissions:

```
# Used to generate client certificate
alloydb.clusters.generateClientCertificate
# Used to fetch connection information
alloydb.instances.connect

```

For impersonating the `alloydb-user` service account, the built-in "Service Account Token Creator" IAM role is broader than necessary. To restrict permissions for that service account, create a custom role that includes only:

```
iam.serviceAccounts.getAccessToken

```

### Custom role for the database user

The `alloydb-user` service account used for database access requires permissions to connect to the instance and authenticate as a database user. Create a custom role with:

```
alloydb.instances.connect
alloydb.users.login
serviceusage.services.use

```

## Troubleshooting

---

**CHECKPOINT: Unable to connect to the database**

Before proceeding, verify: Confirm that you can access the database without errors.

If the check fails, troubleshoot:

Here are some common errors and troubleshooting tips:

- ### Could not find default credentials

  This error can come from either your client application or Teleport. For a client application, ensure that you disable GCP credential loading. Your client should not attempt to load credentials because GCP credentials will be provided by the Teleport Database Service. If you see the credentials error message in the Teleport Database Service logs (at DEBUG log level), then the Teleport Database Service does not have GCP credentials configured correctly. If you are using a service account key, then ensure that the environment variable `GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json` is set and restart your Teleport Database Service to ensure that the env var is available to `teleport`. For example, if your Teleport Database Service runs as a `systemd` service:

  ```
  $ echo 'GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json' | sudo tee -a /etc/default/teleport
  $ sudo systemctl restart teleport
  ```

  See [authentication](https://cloud.google.com/docs/authentication#service-accounts) in the Google Cloud documentation for more information about service account authentication methods.

- ### Unable to cancel a query

  If you use a PostgreSQL cli client like `psql`, and you try to cancel a query with `Ctrl+C`, but it doesn't cancel the query, then you need to connect using a tsh local proxy instead. When `psql` cancels a query, it establishes a new connection without TLS certificates, however Teleport requires TLS certificates not only for authentication, but also to route database connections. If you [enable TLS Routing in Teleport](https://goteleport.com/docs/zero-trust-access/management/tls-routing.md) then `tsh db connect` will automatically start a local proxy for every connection. Alternatively, you can connect via [Teleport Connect](https://goteleport.com/docs/connect-your-client/teleport-clients/teleport-connect.md) which also uses a local proxy. Otherwise, you need to start a tsh local proxy manually using `tsh proxy db` and connect via the local proxy. If you have already started a long-running query in a `psql` session that you cannot cancel with `Ctrl+C`, you can start a new client session to cancel that query manually: First, find the query's process identifier (PID):

  ```
  SELECT pid,usename,backend_start,query FROM pg_stat_activity WHERE state = 'active';

  ```

  Next, gracefully cancel the query using its PID. This will send a SIGINT signal to the postgres backend process for that query:

  ```
  SELECT pg_cancel_backend(<PID>);

  ```

  You should always try to gracefully terminate a query first, but if graceful cancellation is taking too long, then you can forcefully terminate the query instead. This will send a SIGTERM signal to the postgres backend process for that query:

  ```
  SELECT pg_terminate_backend(<PID>);

  ```

  See the PostgreSQL documentation on [admin functions](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL) for more information about the `pg_cancel_backend` and `pg_terminate_backend` functions.

- ### SSL SYSCALL error

  You may encounter the following error when your local `psql` is not compatible with newer versions of OpenSSL:

  ```
  $ tsh db connect --db-user postgres --db-name postgres postgres
  psql: error: connection to server at "localhost" (::1), port 12345 failed: Connection refused
      Is the server running on that host and accepting TCP/IP connections?
  connection to server at "localhost" (127.0.0.1), port 12345 failed: SSL SYSCALL error: Undefined error: 0
  ```

  Please upgrade your local `psql` to the latest version.

---

## Next steps

- Learn more about [IAM authentication for AlloyDB](https://cloud.google.com/alloydb/docs/database-users/manage-iam-auth).
- Learn more about [service account authentication](https://cloud.google.com/docs/authentication#service-accounts) in Google Cloud.
- Learn more about AlloyDB [Auth Proxy permissions](https://cloud.google.com/alloydb/docs/auth-proxy/connect#required-iam-permissions).
