Migrating PostgreSQL cluster hosts to a different availability zone
Managed Service for PostgreSQL cluster hosts reside in Yandex Cloud availability zones. To migrate hosts from one availability zone to another, do the following:
-
Create a subnet in your target availability zone.
-
Add a host to your cluster:
Management consoleCLITerraformREST APIgRPC API-
Go to Managed Service for PostgreSQL.
-
Click the name of your cluster and open the Hosts tab.
-
Click
Create host. -
Specify the following host settings:
- Target availability zone for your hosts.
- New subnet.
- Select Public access to make the host accessible from outside Yandex Cloud, if required.
-
Click Save.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the
yc config set folder-id <folder_ID>command. You can also set a different folder for any specific command using the--folder-nameor--folder-idparameter.Run this command:
yc managed-postgresql host add \ --cluster-name <cluster_name> \ --host zone-id=<availability_zone>,` `subnet-id=<new_subnet_ID>,` `assign-public-ip=<allow_public_access_to_host>You can get the cluster name with the list of clusters in the folder. In the
zone-idparameter, specify the target availability zone for your hosts.-
Add a host resource to the Terraform configuration file describing your infrastructure:
resource "yandex_mdb_postgresql_cluster" "<cluster_name>" { ... host { zone = "<availability_zone>" subnet_id = "<new_subnet_ID>" assign_public_ip = <allow_public_access_to_host> } }In the
zoneparameter, specify the target availability zone for your hosts. -
Check if the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf 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.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the Cluster.AddHosts method, e.g., via the following cURL
request: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>/hosts:batchCreate' \ --data '{ "hostSpecs": [ { "zoneId": "<availability_zone>", "subnetId": "<subnet_ID>", "assignPublicIp": <allow_public_access_to_host> } ] }'You can request the cluster ID with the list of clusters in the folder.
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Call the ClusterService.AddHosts method, e.g., via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/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> } ] }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.ClusterService.AddHostsYou can request the cluster ID with the list of clusters in the folder.
-
Check the server response to make sure your request was successful.
-
-
To connect to the database after migration, specify your new host's FQDN in your backend or client, e.g., in your application code or a GUI IDE. Delete the original host’s FQDN in your source availability zone.
To get the FQDN, request the list of hosts in the cluster:
yc managed-postgresql host list --cluster-name <cluster_name>You will see your host’s FQDN in the command output under
NAME. Alternatively, you can connect using a special FQDN. -
Delete the hosts in the source availability zone:
Management consoleCLITerraformREST APIgRPC API- Go to Managed Service for PostgreSQL.
- Click the name of your cluster and open the Hosts tab.
- Find the host you need in the list, click
in its row, select Delete, and confirm the deletion.
Run the following command for each host:
yc managed-postgresql host delete <host_FQDN> --cluster-name <cluster_name>-
In your Terraform infrastructure configuration file, delete the
hostresource sections with the source availability zone from your cluster’s description. -
Check if the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Type
yesand press Enter.-
Run this command to view the planned changes:
terraform planIf 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.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
-
Call the Cluster.DeleteHosts method, e.g., via the following cURL
request: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>/hosts:batchDelete' \ --data '{ "hostNames": [ "<host_FQDN>" ] }'Where
hostNamesis the array containing the host you want to delete.You can provide only one host FQDN per request. If you need to delete multiple hosts, make a separate request for each of them.
-
Check the server response to make sure your request was successful.
-
Call the ClusterService.DeleteHosts method, e.g., via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/cluster_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "host_names": [ "<host_FQDN>" ] }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.ClusterService.DeleteHostsWhere
host_namesis the array containing the host you want to delete.You can only specify one host FQDN per request. If you need to delete multiple hosts, make a separate request for each of them.
-
Check the server response to make sure your request was successful.
-
Wait for the cluster status to change to Alive. In the management console, go to Managed Service for PostgreSQL. You can see the cluster status in the Availability column.
Note
Not available for clusters with hosts residing in the ru-central1-d availability zone:
- Intel Broadwell platform
- Local SSD storage if using Intel Cascade Lake
Specifics of migration in Yandex Data Transfer
If your cluster is used as an endpoint when transferring data with Data Transfer, and the transfer type is Replication or Snapshot and increment, restart the transfer after migrating the cluster. This way, the transfer will get data about the cluster's new topology.
You do not need to restart Snapshot transfers, as information about the new topology is provided automatically while activating them.
To restart a transfer, choose one of the two methods:
- Deactivate the transfer and wait for its status to change to Stopped. Next, reactivate the transfer and wait for its status to change to Replicating.
- Update any setting for the transfer or endpoint.
For more information, see Migrating a Data Transfer transfer and endpoints to a different availability zone.
Note
Enable the pg_tm_aux extension for your PostgreSQL cluster. This will allow replication to continue following a replacement of the master host. In some cases, a transfer may end in an error after you replace a master in your cluster. For more information, see Troubleshooting.