# CloudSQL - PostgreSQL

Google Cloud SQL PostgreSQL is a fully managed relational database service built for the cloud. It provides a high-performance, scalable, and highly available PostgreSQL database instance without the overhead of managing infrastructure. With Google Cloud SQL, users benefit from Google Cloud's robust infrastructure, which ensures high availability, security, and scalability for their databases.

Through this integration, Apono helps you securely manage access to your Google Cloud SQL PostgreSQL database instances.

To enable Apono to manage Google Cloud SQL PostgreSQL user access, you must create a user and then configure the integration within the Apono UI.

***

### Prerequisites

<table><thead><tr><th width="237">Item</th><th>Description</th></tr></thead><tbody><tr><td><strong>Apono Connector</strong></td><td>On-prem <a href="../apono-connector-for-gcp">connection</a> serving as a bridge between your Google Cloud PostgreSQL databases and Apono<br><br><strong>Minimum Required Version</strong>: 1.4.1<br><br>Use the following steps to <a href="../apono-connector-for-gcp/updating-a-connector-in-google-cloud">update an existing connector</a>.</td></tr><tr><td><strong>Cloud SQL Admin API</strong></td><td><a href="https://cloud.google.com/sql/docs/mysql/admin-api#enable_the_api">API</a> for managing database instances with resources, such as BackupRuns, Databases, and Instances</td></tr><tr><td><strong>Cloud SQL Admin Role</strong></td><td>(Cloud IAM authentication only) Google Cloud role that the Apono connector's service user must have at the instance's project or organization level</td></tr><tr><td><strong>PostgreSQL Info</strong></td><td><p>Information for the database instance to be integrated:</p><ul><li><a href="https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects">Project ID</a></li><li><a href="https://cloud.google.com/bigquery/docs/datasets">Dataset Name</a></li></ul></td></tr></tbody></table>

***

### Create a PostgreSQL user

You must create a user in your PostgreSQL instance for the Apono connector and grant that user permissions to your databases.

{% hint style="danger" %}
You must use the admin account and password to connect to your database.
{% endhint %}

Following these steps to create a user and grant it permissions:

1. In the Google Cloud console, [create a new user](https://cloud.google.com/sql/docs/postgres/create-manage-users#creating) with either **Built-in authentication** or **Cloud IAM** authentication.

{% tabs %}
{% tab title="Built-In Authentication" %}
Use *apono\_connector* for the username.

This authentication method grants the user the `cloudsqlsuperuser` role. Be sure to set a strong password for the user.

{% hint style="success" %}
As an alternative, you can run the following command from your Postgre client:

`CREATE USER 'apono_connector'@'%' IDENTIFIED BY 'password'`
{% endhint %}
{% endtab %}

{% tab title="Cloud IAM" %}
Use *apono-connector-iam-sa@\[PROJECT\_ID].iam.gserviceaccount.com* for the **Principal**.

This authentication method **does not** grant the user account database privileges.

{% hint style="warning" %}
Be sure that the Apono connector GCP service account (*apono-connector-iam-sa@\[PROJECT\_ID].iam.gserviceaccount.com*) has the `Cloud SQL Admin` role.
{% endhint %}
{% endtab %}
{% endtabs %}

2. (Cloud IAM only) In your preferred client tool, grant `cloudsqlsuperuser` access to the user account.

```sql
ALTER ROLE "<CONNECTOR_USERNAME>" WITH CREATEROLE;
GRANT cloudsqlsuperuser TO "<CONNECTOR_USERNAME>";
```

3. In your preferred client tool, grant the `cloudsqlsuperuser` role privileges on all databases except `template0` and `cloudsqladmin`.\
   \
   This allows Apono to perform tasks that are not restricted to a single schema or object within the database, such as creating, altering, and dropping database objects.

{% code overflow="wrap" %}

```sql
DO $$
DECLARE
  database_name text;
BEGIN
  FOR database_name IN (SELECT datname FROM pg_database WHERE datname != 'template0' AND datname != 'cloudsqladmin') LOOP
    EXECUTE 'GRANT ALL PRIVILEGES ON DATABASE ' || quote_ident(database_name) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
  END LOOP;
END; $$

```

{% endcode %}

4. For each database to be managed through Apono, connect to the database and grant `cloudsqlsuperuser` privileges on all objects in the schemas.\
   \
   This allows Apono to perform tasks that are restricted to schemas within the database, such as modifying table structures, creating new sequences, or altering functions.

{% code overflow="wrap" %}

```sql
DO $$
DECLARE
  schema text;
BEGIN
  FOR schema IN (SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT LIKE 'pg_%' AND schema_name != 'information_schema' AND schema_name != 'cron') LOOP
    EXECUTE 'GRANT ALL PRIVILEGES ON SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
    EXECUTE 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
    EXECUTE 'GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
    EXECUTE 'GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
  END LOOP;
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON TABLES TO cloudsqlsuperuser WITH GRANT OPTION';
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON SEQUENCES TO cloudsqlsuperuser WITH GRANT OPTION';
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON FUNCTIONS TO cloudsqlsuperuser WITH GRANT OPTION';
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON SCHEMAS TO cloudsqlsuperuser WITH GRANT OPTION';
END; $$
```

{% endcode %}

5. Connect to the `template1` database and grant `cloudsqlsuperuser` privileges on all objects in the schemas.\
   \
   For any new databases created in the future, this allows Apono to perform tasks that are restricted to schemas within the database, such as modifying table structures, creating new sequences, or altering functions.

{% code overflow="wrap" %}

```sql
DO $$
DECLARE
  schema text;
BEGIN
  FOR schema IN (SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT LIKE 'pg_%' AND schema_name != 'information_schema' AND schema_name != 'cron') LOOP
    EXECUTE 'GRANT ALL PRIVILEGES ON SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
    EXECUTE 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
    EXECUTE 'GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
    EXECUTE 'GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA ' || quote_ident(schema) || ' TO cloudsqlsuperuser WITH GRANT OPTION';
  END LOOP;
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON TABLES TO cloudsqlsuperuser WITH GRANT OPTION';
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON SEQUENCES TO cloudsqlsuperuser WITH GRANT OPTION';
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON FUNCTIONS TO cloudsqlsuperuser WITH GRANT OPTION';
  EXECUTE 'ALTER DEFAULT PRIVILEGES GRANT ALL PRIVILEGES ON SCHEMAS TO cloudsqlsuperuser WITH GRANT OPTION';
END; $$
```

{% endcode %}

6. (Built-in authentication only) [Create a secret](https://docs.apono.io/docs/connectors-and-secrets/apono-integration-secret#gcp) with the credentials from step **1**.

{% hint style="info" %}
When using Cloud IAM authentication, the service account and its permissions are managed through Google Cloud IAM roles and policies. The service account is used to authenticate to the Cloud SQL instance.

**A secret does not need to be created.**
{% endhint %}

***

### Integrate Google Cloud SQL - PostgreSQL

<figure><img src="https://1094436629-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv6MBfUGvblSdAz31yJXm%2Fuploads%2Fgit-blob-90492f18fa2a199c7571b20012fee3b900c0db46%2FCloud-sql-postgresql-1.png?alt=media" alt="" width="563"><figcaption><p>Google Cloud SQL - PostgreSQL</p></figcaption></figure>

{% hint style="success" %}
You can also use the steps below to integrate with Apono using Terraform.

In step **11**, instead of clicking **Confirm**, follow the **Are you integrating with Apono using Terraform?** guidance.
{% endhint %}

Follow these steps to complete the integration:

1. On the [**Catalog**](https://app.apono.io/catalog?search=sql+-+postgreSQL) tab, click **Google Cloud SQL - PostgreSQL**. The **Connect Integration** page appears.
2. Under **Discovery**, click one or more resource types and cloud services to sync with Apono.

{% hint style="info" %}
Apono automatically discovers and syncs all the instances in the environment. After syncing, you can manage access flows to these resources.
{% endhint %}

3. Click **Next**. The **Apono connector** section expands.
4. From the dropdown menu, select a connector.

{% hint style="success" %}
If the desired connector is not listed, click **+ Add new connector** and follow the instructions for creating a [GCP](https://docs.apono.io/docs/gcp-environment/apono-connector-for-gcp) connector.
{% endhint %}

5. Click **Next**. The **Integration Config** section expands.
6. Define the **Integration Config** settings.

   <table><thead><tr><th width="200">Setting</th><th>Description</th></tr></thead><tbody><tr><td><strong>Integration Name</strong></td><td>Unique, alphanumeric, user-friendly name used to identify this integration when constructing an access flow</td></tr><tr><td><strong>Auth Type</strong></td><td><p>Authorization type for the MySQL service account user:</p><ul><li><strong>IAM Auth</strong>: Cloud IAM authentication</li><li><strong>User / Password</strong>: Built-in authentication</li></ul></td></tr><tr><td><strong>Project ID</strong></td><td>ID of the project where the PostgreSQL instance is deployed</td></tr><tr><td><strong>Region</strong></td><td>Location where the PostgreSQL instance is deployed</td></tr><tr><td><strong>Instance ID</strong></td><td>ID of the PostgreSQL instance</td></tr><tr><td><strong>Instance ID User Override</strong></td><td>(Optional) Allows overriding the instance ID for the user</td></tr><tr><td><strong>Database Name</strong></td><td>Name of the database to integrate<br><br>By default, Apono sets this value to <em>postgre</em>.</td></tr><tr><td><strong>SSL Mode</strong></td><td><p>(Optionl) Mode of Secure Sockets Layer (SSL) encryption used to secure the connection with the SQL database server:</p><ul><li><strong>require</strong>: An SSL-encrypted connection must be used.</li><li><strong>allow</strong>: An SSL-encrypted or unencrypted connection is used. If an SSL-encrypted connection is unavailable, the unencrypted connection is used.</li><li><strong>disable</strong>: An unencrypted connection is used.</li><li><strong>prefer</strong>: An SSL-encrypted connection is attempted. If the encrypted connection is unavailable, the unencrypted connection is used.</li><li><strong>verify-ca</strong>: An SSL-encrypted connection must be used and a server certification verification against the provided CA certificates must pass.</li><li><strong>verify-full</strong>: An SSL-encrypted connection must be used and a server certification verification against the provided CA certificates must pass. Additionally, the server hostname is checked against the certificate's names.</li></ul></td></tr></tbody></table>
7. Click **Next**. The **Secret Store** section expands.
8. (User/Password only) [Associate the secret or credentials](https://docs.apono.io/docs/connectors-and-secrets/apono-integration-secret).

{% hint style="info" %}
A secret is **not needed** or Cloud IAM authentication.
{% endhint %}

9. Click **Next**. The **Get more with Apono** section expands.
10. Define the **Get more with Apono** settings.

    <table><thead><tr><th width="202">Setting</th><th>Description</th></tr></thead><tbody><tr><td><strong>Credential Rotation</strong></td><td>(Optional) Number of days after which the database credentials must be rotated<br><br>Learn more about the <a href="../../architecture-and-security/credentials-rotation-policy">Credentials Rotation Policy</a>.</td></tr><tr><td><strong>User cleanup after access is revoked (in days)</strong></td><td><p>(Optional) Defines the number of days after access has been revoked that the user should be deleted</p><p><br>Learn more about <a href="../../architecture-and-security/periodic-user-cleanup-and-deletion">Periodic User Cleanup &#x26; Deletion</a>.</p></td></tr><tr><td><strong>Custom Access Details</strong></td><td>(Optional) Instructions explaining how to access this integration's resources<br><br>Upon accessing an integration, a message with these instructions will be displayed to end users in the User Portal. The message may include up to <strong>400 characters</strong>.<br><br>To view the message as it appears to end users, click <strong>Preview</strong>.</td></tr><tr><td><strong>Integration Owner</strong></td><td><p>(Optional) Fallback approver if no <a href="../../access-flows/dynamic-access-management/resource-and-integration-owners">resource owner</a> is found<br><br>Follow these steps to define one or several integration owners:</p><ol><li>From the <strong>Attribute</strong> dropdown menu, select <strong>User</strong> or <strong>Group</strong> under the relevant identity provider (IdP) platform.</li><li>From the <strong>Value</strong> dropdown menu, select one or multiple users or groups.</li></ol><p><br><strong>NOTE</strong>: When <strong>Resource Owner</strong> is defined, an <strong>Integration Owner</strong> must be defined.</p></td></tr><tr><td><strong>Resource Owner</strong></td><td><p>(Optional) Group or role responsible for managing access approvals or rejections for the resource<br><br>Follow these steps to define one or several <a href="../../access-flows/dynamic-access-management/resource-and-integration-owners">resource owners</a>:</p><ol><li>Enter a <strong>Key name</strong>. This value is the name of the tag created in your cloud environment.</li><li>From the <strong>Attribute</strong> dropdown menu, select an attribute under the IdP platform to which the key name is associated.<br><br>Apono will use the value associated with the key (tag) to identify the resource owner. When you update the membership of the group or role in your IdP platform, this change is also reflected in Apono.</li></ol><p><br><strong>NOTE</strong>: When this setting is defined, an <strong>Integration Owner</strong> must also be defined.</p></td></tr></tbody></table>
11. Click **Confirm**.

<details>

<summary>💡Are you integrating with Apono using Terraform?</summary>

If you want to integrate with Apono using Terraform, follow these steps instead of clicking **Confirm**:

1. At the top of the screen, click **View as Code**. A modal appears with the completed Terraform configuration code.
2. Click to copy the code.
3. Make any additional edits.
4. Deploy the code in your Terraform.

Refer to [Integration Config Metadata](https://docs.apono.io/metadata-for-integration-config/integration-metadata/gcp-cloud-sql-postgresql) for more details about the schema definition.

</details>

Now that you have completed this integration, you can [create access flows](https://docs.apono.io/docs/access-flows/access-flows) that grant permission to your Google Cloud SQL PostgreSQL instance.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.apono.io/docs/gcp-environment/gcp-integrations/cloudsql-postgresql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
