# Installing a CloudSQL database

## Before you begin

* Existing Google Cloud (GCP) integration
* Permissions to create roles and add IAM bindings:
  * `iam.roleAdmin`
  * `iam.securityAdmin`
* Permissions to connect to the Cloud SQL instance, as an authorized user:
  * Grant access (`GRANT`) to other users
  * Create functions (`CREATE FUNCTION`) within the database
* Access to [Google Cloud Shell](https://shell.cloud.google.com/?hl=en_US\&fromcloudshell=true\&show=terminal) and/or [gcloud CLI ](https://cloud.google.com/sdk/docs/install)installed and configured

## Set up PostgreSQL Integration

To set up the PostgreSQL integration:

1. Go to [P0.app](https://p0.app) in your browser. Select **Integrations**, then under the **Resources** section, click **PostgreSQL**.

   <figure><img src="/files/k8u2YLfBtPkCNQLfLwo6" alt=""><figcaption></figcaption></figure>
2. From the list of **Available components**, click **Access management**.

   <figure><img src="/files/0CbJ31nev7UjDNt8gyts" alt=""><figcaption></figcaption></figure>
3. Click **+ Add database**.

   <figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfHe6zVFf8uwXY2fxXgxLKi3Uw2HoR0UsuPHdX60Qr_gyUeGrkiq1obJcwyOgm-i6sb6bFsoYZ6cE6HnDOc6u1vggd5fXYBBJhBmY4gc1Y6rgw158gMkf5CrB9RL4tdwZpk3wwYww?key=pVvJ5tCVLG4RPVSHW9DgTDx7" alt=""><figcaption></figcaption></figure>
4. Enter the **Database identifier**. This is a unique label to identify the database (e.g. `p0-production-db`).

{% hint style="warning" %}
The Database identifier cannot be changed after installation. To change the database identifier the database must be uninstalled from P0 and reinstalled with a new identifier.
{% endhint %}

5. Click on **Google CloudSQL** and populate the details of your **Google CloudSQL** instance and click **Next**.
   * **GCP project ID**: Google Cloud project ID (e.g. `p0-demo`).
   * **GCP region**: Region where the CloudSQL instance is located (e.g. `us-central1`)
   * **CloudSQL instance ID**: ID of the CloudSQL instance which must have public IP access enabled (e.g. `my-cloudsql-instance-001`)
   * **Database name**: Name of the database to manage (e.g. `app_main_db`)

<figure><img src="/files/jzhw3vAbppKPici7pfGG" alt=""><figcaption></figcaption></figure>

6. Ensure P0 can manage user access to PostgreSQL roles. On the resulting **Access management** screen, Step 1 applies to Google Cloud and Steps 2 and 3 apply to Postgres.

   Start by configuring Google Cloud, and copy the commands from **Step 1**.

   <figure><img src="/files/Vh7PBGHNIZEjg5CBSOBE" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Keep the **Access management** browser tab open. You'll need to copy information from this page in subsequent steps.
{% endhint %}

7. Open the [Google Cloud Shell](https://shell.cloud.google.com/?hl=en_US\&fromcloudshell=true\&show=terminal) in a new browser tab, and paste and run the copied commands.

{% hint style="info" %}
Keep this browser tab open as well. The next steps require you to toggle back and forth, between the P0.app's **Access management** page and the [Google Cloud Shell](https://shell.cloud.google.com/?show=ide%2Cterminal) browser tabs.
{% endhint %}

<figure><img src="/files/0ZJaA9t8qDy7VgUmPutT" alt="" width="563"><figcaption></figcaption></figure>

8. From Google Cloud Shell, connect to your Cloud SQL PostgreSQL instance using the psql command. Replace `REGION` with your actual region (for example, `us-central1`, `us-east1`, etc.).

<figure><img src="/files/nAlYOYVmRvnwmPVZetAL" alt=""><figcaption></figcaption></figure>

9. Switch back to the **Access management** browser tab and copy the commands from **Step 2** and **Step 3**.

<figure><img src="/files/HsHJio1iDKfLGB2F0IDD" alt="" width="563"><figcaption></figcaption></figure>

10. Return to Google Cloud Shell and run the copied SQL commands.

<figure><img src="/files/yqOJbFhqUdjy3lvUkQ11" alt="" width="563"><figcaption></figcaption></figure>

11. After running the commands, switch back to the **Access management** browser tab and click **Next**.

<figure><img src="/files/gtCloinnPBDQtyrS3TCe" alt=""><figcaption></figcaption></figure>

12. Click **Finish** to finalize your configuration.

<figure><img src="/files/JAbMxLt158T54lws99Q4" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
You can manage your connections (e.g. [request access](/integrations/resource-integrations/postgresql/requesting-access.md) to this PostgreSQL instance through Slack) on the PostgreSQL **Integrations** page and add new configurations as needed.
{% endhint %}

<figure><img src="/files/An8aqpVLCNUrrejJxAp4" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
Congratulations! You've now set up PostgreSQL for P0.
{% endhint %}


---

# 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.p0.dev/integrations/resource-integrations/postgresql/installing-an-cloudsql-database.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.
