[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Sharded PostgreSQL](../index.md) > [Step-by-step guides](index.md) > Managing cluster hosts

# Managing Sharded PostgreSQL cluster hosts

You can manage the Managed Service for Sharded PostgreSQL cluster hosts, including the following operations:

* [Get a list of hosts](#list).
* [Create a host](#add).
* [Update host settings](#update).
* [Delete a host](#remove).

## Getting a list of cluster hosts {#list}

{% list tabs group=instructions %}

- Management console {#console}

  1. Navigate to **Yandex Managed Service for Sharded&nbsp;PostgreSQL**.
  1. Click the name of your cluster and select the **Hosts** 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 hosts, run this command:

  ```bash
  yc managed-sharded-postgresql hosts list \
    --cluster-name <cluster_name>
  ```

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

  Result:

  
  ```text
  +----------------------------+----------------------+---------+--------+--------------------+-----------+
  |            NAME            |      CLUSTER ID      |  ROLE   | HEALTH |      ZONE ID       | PUBLIC IP |
  +----------------------------+----------------------+---------+--------+--------------------+-----------+
  | rc1b***mdb.yandexcloud.net | c9qp71dk1dfg******** | PRIMARY | ALIVE  | ru-central1-b      | true      |
  | rc1a***mdb.yandexcloud.net | c9qp71dk1dfg******** | PRIMARY | ALIVE  | ru-central1-a      | false     |
  +----------------------------+----------------------+---------+--------+--------------------+-----------+
  ```


- 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 [Cluster.ListHosts](../api-ref/Cluster/listHosts.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-spqr/v1/clusters/<cluster_ID>/hosts'
     ```

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

  1. View the [server response](../api-ref/Cluster/listHosts.md#yandex.cloud.mdb.spqr.v1.ListClusterHostsResponse) 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 [ClusterService.ListHosts](../api-ref/grpc/Cluster/listHosts.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/spqr/v1/cluster_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.spqr.v1.ClusterService.ListHosts
     ```

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

  1. Check the [server response](../api-ref/grpc/Cluster/listHosts.md#yandex.cloud.mdb.spqr.v1.ListClusterHostsResponse) to make sure your request was successful.

{% endlist %}

## Creating a host {#add}

The number of `INFRA`, `ROUTER`, and `COORDINATOR` hosts in the Managed Service for Sharded PostgreSQL clusters is limited by the CPU and RAM quotas available to database clusters in your cloud. To review your resource usage, open the [Quotas](https://console.yandex.cloud/cloud?section=quotas) page and find the **Managed Databases** section.

{% list tabs group=instructions %}

- Management console {#console}

  To create a host:
  1. Navigate to **Yandex Managed Service for Sharded&nbsp;PostgreSQL**.
  1. Click the cluster name and navigate to the **Hosts** tab.
  1. Click **Create host**.

  
  1. Specify the following host settings:
     * Availability zone.
     * Subnet (if the required subnet is not on the list, [create it](../../vpc/operations/subnet-create.md)).
     * [Host type](../concepts/index.md#router): `INFRA` (for a cluster with standard sharding), `ROUTER`, or `COORDINATOR` (for a cluster with advanced sharding).
     * To make the host accessible from outside Yandex Cloud, select **Public access**.


- 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 host:

  
  1. To choose a subnet for your new host, get the list of all cluster subnets:

     ```bash
     yc vpc subnet list
     ```

     Result:

     ```text
     +----------------------+-----------+----------------------+---------------+-------------------+
     |          ID          |   NAME    |      NETWORK ID      |      ZONE     |      RANGE        |
     +----------------------+-----------+----------------------+---------------+-------------------+
     | b0cl69q1w2e3******** | default-d | enp6rq71w2e3******** | ru-central1-d | [172.16.**.**/20] |
     | e2lkj9q1w2e3******** | default-b | enp6rq71w2e3******** | ru-central1-b | [10.10.**.**/16]  |
     | e9b9v2q1w2e3******** | default-a | enp6rq71w2e3******** | ru-central1-a | [172.16.**.**/20] |
     +----------------------+-----------+----------------------+---------------+-------------------+
     ```

     If the required subnet is not on the list, [create it](../../vpc/operations/subnet-create.md).


  1. See the description of the CLI command for adding new hosts:

     ```bash
     yc managed-sharded-postgresql hosts add --help
     ```

  1. Run this command to add a host:

     
     ```bash
     yc managed-sharded-postgresql hosts add \
       --cluster-name <cluster_name> \
       --host zone-id=<availability_zone>,subnet-id=<subnet_ID>,type=<host_type>
     ```


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

     
     You should specify the subnet ID if the availability zone contains more than one subnet; otherwise, Managed Service for Sharded PostgreSQL will automatically select the only subnet.


     Possible [host type](../concepts/index.md#router) values: `infra` (for a cluster with standard sharding), `router`, or `coordinator` (for a cluster with advanced sharding).

     You can also set the additional `assign-public-ip` option in `--host` to manage public access to the host:
       * `true`: Public access enabled.
       * `false`: Public access disabled.

- 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 [Cluster.AddHosts](../api-ref/Cluster/addHosts.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-spqr/v1/clusters/<cluster_ID>/hosts:batchCreate' \
       --data '{
                 "hostSpecs": [
                   {
                     "zoneId": "<availability_zone>",
                     "subnetId": "<subnet_ID>",
                     "assignPublicIp": <allow_public_access_to_host>,
                     "type": "<host_type>"
                   },
                   { <similar_settings_for_new_host_2> },
                   { ... },
                   { <similar_settings_for_new_host_N> }
                 ]
               }'
     ```

     Where:

     * `<cluster_ID>`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
     * `hostSpecs` is an array of new hosts. Each array element contains the configuration for a single host and has the following structure:

       * `zoneId`: Availability zone.
       * `subnetId`: Subnet ID.
       * `assignPublicIp`: Host accessibility from the internet via a public IP address, `true` or `false`.
       * `type`: [Host type](../concepts/index.md#router), `infra` (for a cluster with standard sharding), `router`, or `coordinator` (for a cluster with advanced sharding).

  1. Check the [server response](../api-ref/Cluster/addHosts.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 [ClusterService.AddHosts](../api-ref/grpc/Cluster/addHosts.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/spqr/v1/cluster_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "host_specs": [
               {
                 "zone_id": "<availability_zone>",
                 "subnet_id": "<subnet_ID>",
                 "assign_public_ip": <allow_public_access_to_host>,
                 "type": "<host_type>"
               },
               { <similar_settings_for_new_host_2> },
               { ... },
               { <similar_settings_for_new_host_N> }
             ]
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.spqr.v1.ClusterService.AddHosts
     ```

     Where:

     * `cluster_id`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
     * `host_specs` is an array of new hosts. Each array element contains the configuration for a single host and has the following structure:

       * `zone_id`: Availability zone.
       * `subnet_id`: Subnet ID.
       * `assign_public_ip`: Host accessibility from the internet via a public IP address, `true` or `false`.
       * `type`: [Host type](../concepts/index.md#router), `infra` (for a cluster with standard sharding), `router`, or `coordinator` (for a cluster with advanced sharding).



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

{% endlist %}


## Updating a host {#update}

You can change public access settings for any host in a Managed Service for Sharded PostgreSQL cluster.

{% list tabs group=instructions %}

- 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 update the cluster host’s settings, run the following command:

  ```bash
  yc managed-sharded-postgresql hosts update <host_name> \
    --cluster-name <cluster_name> \
    --assign-public-ip=<public_access_to_host>
  ```

  Where:

  * `cluster-name`: Cluster name.

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

  * `assign-public-ip`: Public access to the host, `true` or `false`.

  You can get the host name with the [list of hosts in the cluster](#list).

- 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 [Cluster.UpdateHosts](../api-ref/Cluster/updateHosts.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 POST \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --header "Content-Type: application/json" \
       --url 'https://mdb.api.cloud.yandex.net/managed-spqr/v1/clusters/<cluster_ID>/hosts:batchUpdate' \
       --data '{
                 "updateHostSpecs": [
                   {
                     "updateMask": "assignPublicIp",
                     "hostName": "<host_name>",
                     "assignPublicIp": <allow_public_access_to_host>
                   },
                   { <similar_settings_for_host_2_being_changed> },
                   { ... },
                   { <similar_settings_for_host_N_being_changed> }
                 ]
               }'
     ```

     Where: 

     * `<cluster_ID>`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
     * `updateHostSpecs`: Array of hosts you are updating. Each array element contains the configuration for a single host and has the following structure:

       * `updateMask`: Comma-separated string of settings to update.
       * `hostName`: Target host name.
       * `assignPublicIp`: Internet access to the host via a public IP address, `true` or `false`.

     You can get the host name with the [list of hosts in the cluster](#list).

  1. Check the [server response](../api-ref/Cluster/updateHosts.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 [ClusterService.UpdateHosts](../api-ref/grpc/Cluster/updateHosts.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/spqr/v1/cluster_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "update_host_specs": [
               {
                 "update_mask": {
                     "paths": [
                       "assign_public_ip"
                     ]
                 },
                 "host_name": "<host_name>",
                 "assign_public_ip": <allow_public_access_to_host>
               },
               { <similar_settings_for_host_2_being_changed> },
               { ... },
               { <similar_settings_for_host_N_being_changed> }
             ]
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.postgresql.v1.ClusterService.UpdateHosts
     ```

     Where:

     * `cluster_id`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
     * `update_host_specs`: Array of hosts you are updating. Each array element contains the configuration for a single host and has the following structure:

       * `update_mask`: List of settings to update as an array of strings (`paths[]`).
       * `host_name`: Target host name.
       * `assign_public_ip`: Internet access to the host via a public IP address, `true` or `false`.

     You can get the host name with the [list of hosts in the cluster](#list).

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

{% endlist %}


## Deleting a host {#remove}

You can remove an `INFRA`, `ROUTER`, or `COORDINATOR` type host from a Sharded PostgreSQL cluster if it is not the cluster’s only host of that type. To replace the only host, first create a new host and then delete the old one.

{% list tabs group=instructions %}

- Management console {#console}

  To delete a host from a cluster:
  1. Navigate to **Yandex Managed Service for Sharded&nbsp;PostgreSQL**.
  1. Click the name of your cluster and select the **Hosts** tab.
  1. Click ![image](../../_assets/console-icons/ellipsis.svg) next to the host you want to delete, select **Delete**, and confirm 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 host from a cluster, run this command:

  ```bash
  yc managed-sharded-postgresql hosts delete <host_name> \
    --cluster-name <cluster_name>
  ```

  You can get the host name with the [list of hosts in the cluster](#list).

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

- 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 [Cluster.DeleteHosts](../api-ref/Cluster/deleteHosts.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-spqr/v1/clusters/<cluster_ID>/hosts:batchDelete' \
       --data '{
                 "hostNames": [
                   "<host_name>"
                 ]
               }'
     ```

     Where:

     * `<cluster_ID>`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
     * `hostNames`: Array of strings consisting of a single element with the host name. You can get the host name with the [list of hosts in the cluster](#list).

  1. Check the [server response](../api-ref/Cluster/deleteHosts.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 [ClusterService.DeleteHosts](../api-ref/grpc/Cluster/deleteHosts.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/spqr/v1/cluster_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "host_names": [
               "<host_name>"
             ]
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.spqr.v1.ClusterService.DeleteHosts
     ```

     Where: 

     * `cluster_id`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
     * `host_names`: Array of strings consisting of a single element with the host name. You can get the host name with the [list of hosts in the cluster](#list).

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

{% endlist %}