# Managing databases in Managed Service for PostgreSQL

You can add, rename, and delete databases, and view their info.

{% note warning %}

You can't manage databases using SQL commands.

{% endnote %}

## Getting a list of cluster databases {#list-db}

{% list tabs group=instructions %}

- Management console {#console}

  1. Navigate to **Managed Service for&nbsp;PostgreSQL**.
  1. Click the name of your cluster and select the **Databases** tab.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To get a list of cluster databases, run this command:

  ```bash
  yc managed-postgresql database list --cluster-name=<cluster_name>
  ```

  You can get the cluster name with the [list of clusters in the folder](cluster-list.md#list-clusters).

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Database.List](../api-ref/Database/list.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request GET \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/databases'
     ```

     You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/Database/list.md#yandex.cloud.mdb.postgresql.v1.ListDatabasesResponse) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.List](../api-ref/grpc/Database/list.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.postgresql.v1.DatabaseService.List
     ```

     You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/grpc/Database/list.md#yandex.cloud.mdb.postgresql.v1.ListDatabasesResponse) to make sure your request was successful.

{% endlist %}

## Creating a database {#add-db}

{% note info %}

You can create a maximum of 1000 databases in each cluster.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  1. Navigate to **Managed Service for&nbsp;PostgreSQL**.
  1. Click the cluster name.
  1. If necessary, [create](cluster-users.md#adduser) a new user account for database owner.
  1. Select the **Databases** tab.
  1. Click **Create database**.
  1. Specify database settings:

      * Name

        The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

      * Owner

      * Deletion protection

        Possible values:
          - **Same as cluster**
          - **Enabled**
          - **Disabled**

      * Template: The name of an existing database to use as a template for the new database’s schema. This is an optional setting. Creating the new database will close all active connections to the template database.

          For more information, see [this PostgreSQL guide](https://www.postgresql.org/docs/current/sql-createdatabase.html).

      * Locales for collation and character set.

          PostgreSQL supports multiple locales. The choice of locale affects the following:
          
          - Sort order in queries using the `ORDER BY` clause or standard text comparison operators.
          - `upper`, `lower`, `initcap`, and `to_char` functions.
          - Pattern-matching operators, e.g., `LIKE`, `ILIKE`, `SIMILAR TO`, and regular expressions.
          - Support of indexes with the `LIKE` operator.
          
          The default locale is `C`. When using the `C` encoding for text data with non-Latin, e.g., Cyrillic, characters, you may encounter errors in data sorting and incorrect pattern-matching results. If this locale causes incorrect data handling in your database, select a different one from the list. However, note that using a non-standard locale may reduce database query performance.
          
          To learn more about locale settings, see the [this PostgreSQL article](https://www.postgresql.org/docs/current/locale.html).
          
          Once you create a database, you cannot change its locale settings. However, you can set a collation for columns when creating or altering individual tables. For more information, see the relevant [PostgreSQL guides](https://www.postgresql.org/docs/current/sql-createtable.html).

  1. Click **Create**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To create a database in a cluster:

  1. See the description of the CLI command for creating a database:

     ```bash
     yc managed-postgresql database create --help
     ```

  1. Request a list of cluster users to select the owner for the new database:

     ```bash
     yc managed-postgresql user list --cluster-name=<cluster_name>
     ```

     If the required user is not in the list, [create it](cluster-users.md#adduser).

  1. Run the database creation command. Optionally, specify the template and your preferred collation and character set locales. The default locale settings are `LC_COLLATE=C` and `LC_CTYPE=C`:

     ```bash
     yc managed-postgresql database create <DB_name> \
        --cluster-name=<cluster_name> \
        --owner=<DB_owner_name> \
        --lc-collate=<collation_locale> \
        --lc-type=<character_set_locale> \
        --template-db=<DB_template_name>
     ```

     The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

     You can get the cluster name from the [list of clusters in your folder](cluster-list.md#list-clusters).

     Managed Service for PostgreSQL will start creating the database.

- Terraform {#tf}

    1. Open the current Terraform configuration file with the infrastructure plan.

        For more on how to create this file, see [Creating a cluster](cluster-create.md).

        For a complete list of configurable Managed Service for PostgreSQL cluster database fields, see [this Terraform provider guide](../../terraform/resources/mdb_postgresql_database.md).

    1. Add the `yandex_mdb_postgresql_database` resource: Optionally, specify the template and your preferred collation and character set locales. The default locale settings are `LC_COLLATE=C` and `LC_CTYPE=C`:

        ```hcl
        resource "yandex_mdb_postgresql_database" "<DB_name>" {
          cluster_id  = "<cluster_ID>"
          name        = "<DB_name>"
          owner       = "<DB_owner_name>"
          lc_collate  = "<collation_locale>"
          lc_type     = "<character_set_locale>"
          template_db = "<DB_template_name>"
          deletion_protection = <deletion_protection>
        }
        ```

        Where:
          * `owner`: Username of the owner that must be specified in the `yandex_mdb_postgresql_user` resource.
          * `deletion_protection`: Database deletion protection, `true`, `false`, or `unspecified` (inherits the value from the cluster). The default value is `unspecified`.

        The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

    1. Make sure the settings are correct.

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes.

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

  {% note warning %}

  Changing the owner of the existing database by editing the `owner` setting will trigger the database recreation, resulting in data loss.

  {% endnote %}

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Database.Create](../api-ref/Database/create.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request POST \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --header "Content-Type: application/json" \
       --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/databases' \
       --data '{
                 "databaseSpec": {
                   "name": "<DB_name>",
                   "owner": "<DB_owner_name>",
                   "lcCollate": "<collation_locale>",
                   "lcCtype": "<character_set_locale>",
                   "extensions": [
                     {
                       "name": "<extension_name>"
                     }
                   ],
                   "deletionProtection": <deletion_protection>
                 }
               }'
     ```

     Where `databaseSpec` is the object containing the settings for the new database and having the the following structure:

     * `name`: Database name.

       The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

     * `owner`: Database owner.
     * `lcCollate`: Collation locale. The default value is `C`.
     * `lcCtype`: Character set locale. The default value is `C`.
     * `extensions`: Array of database extensions. Each array element contains the configuration for a single extension and has the following structure:

       * `extensions.name`: Extension name.

       Give a name according to the [list of supported PostgreSQL extensions and utilities](extensions/cluster-extensions.md#postgresql).

     * `deletionProtection`: Database deletion protection, `true`, `false`, or `unspecified` (inherits the value from the cluster). The default value is `unspecified`.

     You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/Database/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.Create](../api-ref/grpc/Database/create.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "database_spec": {
               "name": "<DB_name>",
               "owner": "<DB_owner_name>",
               "lc_collate": "<collation_locale>",
               "lc_ctype": "<character_set_locale>",
               "extensions": [
                 {
                   "name": "<extension_name>"
                 }
               ],
               "deletion_protection": <deletion_protection>
             }
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.postgresql.v1.DatabaseService.Create
     ```

     Where `database_spec` are the new database settings:

     * `name`: Database name.

       The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

     * `owner`: Database owner.
     * `lc_collate`: Collation locale. The default value is `C`.
     * `lc_ctype`: Character set locale. The default value is `C`.
     * `extensions`: Array of database extensions. Each array element contains the configuration for a single extension and has the following structure:

       * `extensions.name`: Extension name.

       Use a name from  the [list of supported PostgreSQL extensions and utilities](extensions/cluster-extensions.md#postgresql).

     * `deletion_protection`: Database deletion protection, `true`, `false`, or `unspecified` (inherits the value from the cluster). The default value is `unspecified`.

     You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/grpc/Database/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

## Renaming a database {#rename-db}

{% list tabs group=instructions %}

- Terraform {#tf}

  1. Open the current Terraform configuration file with the infrastructure plan.

      For more on how to create this file, see [Creating a cluster](cluster-create.md).

      For a complete list of configurable Managed Service for PostgreSQL cluster database fields, see [this Terraform provider guide](../../terraform/resources/mdb_postgresql_database.md).

  1. Find the `yandex_mdb_postgresql_database` resource describing your target database.
  1. Edit the `name` field value:

      ```hcl
      resource "yandex_mdb_postgresql_database" "<database_name>" {
        ...
        name     = "<new_database_name>"
        ...
      }
      ```

      The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

  1. Make sure the settings are correct.

      1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
      1. Run this command:
      
         ```bash
         terraform validate
         ```
      
         Terraform will show any errors found in your configuration files.

  1. Confirm updating the resources.

      1. Run this command to view the planned changes:
      
         ```bash
         terraform plan
         ```
      
         If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
      
      1. If everything looks correct, apply the changes:
         1. Run this command:
      
            ```bash
            terraform apply
            ```
      
         1. Confirm updating the resources.
         1. Wait for the operation to complete.

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Database.Update](../api-ref/Database/update.md) method, e.g., via the following [cURL](https://curl.se/) request:

     {% note warning %}
     
     The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
     
     {% endnote %}

     ```bash
     curl \
       --request PATCH \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --header "Content-Type: application/json" \
       --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/databases/<previous_DB_name>' \
       --data '{
                 "updateMask": "newDatabaseName",
                 "newDatabaseName": "<new_DB_name>"
               }'
     ```

     Where:

     * `updateMask`: Comma-separated string of settings to update.

       Here, we provide only one setting.

     * `newDatabaseName`: New database name.

       The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

     You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the database name, with the [list of databases in the cluster](#list-db).

  1. Check the [server response](../api-ref/Database/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.Update](../api-ref/grpc/Database/update.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     {% note warning %}
     
     The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `update_mask` parameter as an array of `paths[]` strings.
     
     {% cut "Format for listing settings" %}
     
     ```yaml
     "update_mask": {
         "paths": [
             "<setting_1>",
             "<setting_2>",
             ...
             "<setting_N>"
         ]
     }
     ```
     
     {% endcut %}
     
     {% endnote %}

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "database_name": "<previous_DB_name>",
             "update_mask": {
               "paths": [
                 "new_database_name"
               ]
             },
             "new_database_name": "<new_DB_name>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.postgresql.v1.DatabaseService.Update
     ```

     Where:

     * `update_mask`: List of settings you want to update as an array of strings (`paths[]`).

       Here, we provide only one setting.

     * `new_database_name`: New database name.

       The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names `postgres`, `template0`, and `template1` are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.

     You can get the cluster ID from the [list of clusters in your folder](cluster-list.md#list-clusters), and the database name, from the [list of databases in your cluster](#list-db).

  1. Check the [server response](../api-ref/grpc/Database/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

## Configuring deletion protection {#update-db-deletion-protection}

{% list tabs group=instructions %}

- Management console {#console}

  1. Navigate to **Managed Service for&nbsp;PostgreSQL**.
  1. Click the name of your cluster and select the **Databases** tab.
  1. Click ![image](../../_assets/console-icons/ellipsis.svg) in the relevant database row and select **Configure**.
  1. Select your preferred option in the **Deletion protection** field.
  1. Click **Save**.

- Terraform {#tf}

  1. Open the current Terraform configuration file with the infrastructure plan.

  1. Find the `yandex_mdb_postgresql_database` resource describing your target database.

  1. Add the `deletion_protection` attribute. The possible values are `true`, `false`, or `unspecified` (inherits the value from the cluster). The default value is `unspecified`.

        ```hcl
        resource "yandex_mdb_postgresql_database" "<DB_name>" {
          ...
          deletion_protection = <deletion_protection>
          ...
        }
        ```

  1. Make sure the settings are correct.

      1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
      1. Run this command:
      
         ```bash
         terraform validate
         ```
      
         Terraform will show any errors found in your configuration files.

  1. Confirm updating the resources.

      1. Run this command to view the planned changes:
      
         ```bash
         terraform plan
         ```
      
         If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
      
      1. If everything looks correct, apply the changes:
         1. Run this command:
      
            ```bash
            terraform apply
            ```
      
         1. Confirm updating the resources.
         1. Wait for the operation to complete.

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Database.Update](../api-ref/Database/update.md) method, e.g., via the following [cURL](https://curl.se/) request:

     {% note warning %}
     
     The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
     
     {% endnote %}

     ```bash
     curl \
       --request PATCH \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --header "Content-Type: application/json" \
       --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/databases/<DB_name>' \
       --data '{
                 "updateMask": "deletionProtection",
                 "deletionProtection": <deletion_protection>
               }'
     ```

     Where:

     * `updateMask`: Comma-separated string of settings to update.

       Here, we provide only one setting.

     * `deletionProtection`: Database deletion protection, `true`, `false`, or `unspecified` (inherits the value from the cluster). The default value is `unspecified`.

     You can get the cluster ID from the [list of clusters in your folder](cluster-list.md#list-clusters), and the database name, from the [list of databases in your cluster](#list-db).

  1. Check the [server response](../api-ref/Database/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.Update](../api-ref/grpc/Database/update.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     {% note warning %}
     
     The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `update_mask` parameter as an array of `paths[]` strings.
     
     {% cut "Format for listing settings" %}
     
     ```yaml
     "update_mask": {
         "paths": [
             "<setting_1>",
             "<setting_2>",
             ...
             "<setting_N>"
         ]
     }
     ```
     
     {% endcut %}
     
     {% endnote %}

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "database_name": "<DB_name>",
             "update_mask": {
               "paths": [
                 "deletion_protection"
               ]
             },
             "deletion_protection": <deletion_protection>
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.postgresql.v1.DatabaseService.Update
     ```

     Where:

     * `update_mask`: List of settings you want to update as an array of strings (`paths[]`).

       Here, we provide only one setting.

     * `deletion_protection`: Database deletion protection, `true`, `false`, or `unspecified` (inherits the value from the cluster). The default value is `unspecified`.

     You can get the cluster ID from the [list of clusters in your folder](cluster-list.md#list-clusters), and the database name, from the [list of databases in your cluster](#list-db).

  1. Check the [server response](../api-ref/grpc/Database/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

{% note warning %}

Deletion protection only applies to individual databases. Deleting a cluster will permanently remove all its databases, including those with deletion protection.

{% endnote %}

## Deleting a database {#remove-db}

A database can have deletion protection enabled. To delete it, first [disable deletion protection](#update-db-deletion-protection).

{% list tabs group=instructions %}

- Management console {#console}

  To delete a database:
  1. Navigate to **Managed Service for&nbsp;PostgreSQL**.
  1. Click the name of your cluster and select the **Databases** tab.
  1. Find the database you need in the list, click ![image](../../_assets/console-icons/ellipsis.svg) in its row, select **Delete**, then confirm the deletion.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To delete a database, run this command:

  ```bash
  yc managed-postgresql database delete <DB_name> \
     --cluster-name <cluster_name>
  ```

  You can get the cluster name with the [list of clusters in the folder](cluster-list.md).

- Terraform {#tf}

  To delete a database:
  1. Open the current Terraform configuration file with the infrastructure plan.

     For more on how to create this file, see [Creating a cluster](cluster-create.md).

     For a complete list of configurable Managed Service for PostgreSQL cluster database fields, see [this Terraform provider guide](../../terraform/resources/mdb_postgresql_database.md).

  1. Remove the `yandex_mdb_postgresql_database` resource with the name of the database you are deleting.

  1. Make sure the settings are correct.

      1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
      1. Run this command:
      
         ```bash
         terraform validate
         ```
      
         Terraform will show any errors found in your configuration files.

  1. Confirm updating the resources.

      1. Run this command to view the planned changes:
      
         ```bash
         terraform plan
         ```
      
         If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
      
      1. If everything looks correct, apply the changes:
         1. Run this command:
      
            ```bash
            terraform apply
            ```
      
         1. Confirm updating the resources.
         1. Wait for the operation to complete.

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Database.Delete](../api-ref/Database/delete.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request DELETE \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/databases/<DB_name>'
     ```

     You can get the cluster ID from the [list of clusters in your folder](cluster-list.md#list-clusters), and the database name, from the [list of databases in your cluster](#list-db).

  1. Check the [server response](../api-ref/Database/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.Delete](../api-ref/grpc/Database/delete.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "database_name": "<DB_name>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.postgresql.v1.DatabaseService.Delete
     ```

     You can get the cluster ID from the [list of clusters in your folder](cluster-list.md#list-clusters), and the database name, from the [list of databases in your cluster](#list-db).

  1. Check the [server response](../api-ref/grpc/Database/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

{% note warning %}

If you plan to create a new database with the same name, wait for the deletion to complete. Otherwise, the system will restore the original database. You can check the operation status in the [list of cluster operations](cluster-list.md#list-operations).

{% endnote %}