> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bqto.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloud SQL

> Connect to a private Cloud SQL instance for PostgreSQL, MySQL or SQL Server, including read replicas, through a bqto port forward.

bqto connects to Cloud SQL instances with a private IP, through private services access (PSA) or
Private Service Connect (PSC). Read replicas work too. This service is in the free bqto.

## Connect

```sh theme={null}
bqto connect --service cloudsql --project my-project --instance sql-main
```

`--instance` takes the name of the instance. `cloudsql` is the default service, so you can leave
out `--service`.

bqto gives you a port forward. The local port is the port of the instance plus 10000, so 5432
becomes 15432. The `try:` line gives a command for the client:

```text theme={null}
ready: 127.0.0.1:15432 → 10.0.0.5:5432 (instance sql-main), bastion bqto-13f6cae5, expires at 19:30 (in 6m)
try: psql "host=127.0.0.1 port=15432 user=postgres"
```

## Clients

| Engine | Client |
| - | - |
| PostgreSQL | `psql` with `$PGHOST` and `$PGPORT` |
| MySQL or SQL Server | `mysql` or `sqlcmd` with `$BQTO_HOST` and `$BQTO_PORT` |

When you run a command through the session with `-- CMD`, the command gets these variables:

```sh theme={null}
bqto connect --service cloudsql --project my-project --instance sql-main -- sh -c 'psql -h "$PGHOST" -p "$PGPORT" -U app'
```

The database login is separate from bqto. bqto carries no database password. You type it into
your own client.

## What it needs

* The API `sqladmin.googleapis.com` in the service project.
* The role `roles/cloudsql.viewer` in the service project, together with the roles that every
  session needs. See [Permissions](https://docs.bqto.dev/reference/permissions.md).

## Connection logs

To match a connection in the database to a session, turn on the connection log of the instance.
These flags come from Google's documentation and were not tested live.

```sh theme={null}
# Cloud SQL for PostgreSQL
gcloud sql instances patch INSTANCE --project $PROJECT --database-flags=log_connections=on

# Cloud SQL for MySQL keeps it in the general log
gcloud sql instances patch INSTANCE --project $PROJECT --database-flags=general_log=on,log_output=FILE
```

Each flag writes to the logs of the instance in Cloud Logging. Each has a cost in log volume. For
MySQL, the flag also costs instance performance.

## Check the server certificate

With [`--endpoint`](https://docs.bqto.dev/services/private-ip), bqto prints a `try:` line with `sslmode=require`. That
mode encrypts the connection, but it does not check a certificate. To check the certificate,
download the server CA of the instance into `server-ca.pem`.

With the default per-instance CA, each instance has its own CA, so it is enough to check the CA.
The certificate holds the DNS name of the instance only on Private Service Connect instances.

```sh theme={null}
psql "host=127.0.0.1 port=15432 user=postgres sslmode=verify-ca sslrootcert=server-ca.pem"
```

With the shared CA or a customer-managed CA, the certificate holds the DNS name of the instance.
With the shared CA, check the hostname too. libpq checks `host` and connects to `hostaddr`:

```sh theme={null}
psql "host=DNS_NAME hostaddr=127.0.0.1 port=15432 user=postgres sslmode=verify-full sslrootcert=server-ca.pem"
```

See [Cloud SQL server CA modes](https://docs.cloud.google.com/sql/docs/postgres/authorize-ssl).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.