Migrating PostgreSQL clusters
To migrate your database to Yandex Managed Service for PostgreSQL, you need to directly transfer the data, acquire a write lock for the old database, and switch over the load to the database cluster in Yandex Cloud.
Performing migration with Data Transfer lets you:
- Go without creating an intermediate VM or granting online access to your Managed Service for PostgreSQL target cluster.
- Minimize the DB downtime when migrating data.
- Migrate from older PostgreSQL versions to newer versions.
For more information, see What tasks Yandex Data Transfer is used for.
To use this migration method, allow connecting to the source cluster from the internet.
Transferring data
-
Prepare the infrastructure:
ManuallyTerraform-
Create a Managed Service for PostgreSQL target cluster in any suitable configuration. Note that:
- The PostgreSQL version must be the same or higher than the version in the source cluster. You cannot perform migration while downgrading PostgreSQL version.
- When creating a cluster, specify the same database name as in the source cluster.
- Enable the same PostgreSQL extensions as in the source cluster.
-
Create a source endpoint with the following parameters:
- Database type:
PostgreSQL
. - Endpoint parameters → Connection settings:
Custom installation
Specify the parameters for connecting to the source cluster.
- Database type:
-
Create a target endpoint with the following parameters:
- Database type:
PostgreSQL
. - Endpoint parameters → Connection settings:
Managed Service for PostgreSQL cluster
.
Specify the ID of the target cluster.
- Database type:
-
Create a transfer of the Snapshot and increment type that will use the created endpoints.
-
Warning
Abstain from making any changes to the data schema in the source and target clusters when the data transfer is running. For more information, see Working with databases during transfer.
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the data-transfer-pgsql-mpg.tf
configuration file to the same working directory.This file describes:
- Network.
- Subnet.
- Security group and the rule required to connect to a cluster.
- Managed Service for PostgreSQL target cluster.
- Source endpoint.
- Target endpoint.
- Transfer.
-
Specify the following in
data-transfer-pgsql-mpg.tf
:-
pg-extensions
: List of PostgreSQL extensions in the source cluster. -
Target cluster parameters also used as target endpoint parameters:
target_mysql_version
: PostgreSQL version, must be the same as or higher than the version in the source cluster.target_user
andtarget_password
: Database owner username and password.
-
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Create the required infrastructure:
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
.Once created, your transfer will be activated automatically.
-
-
-
Wait for the transfer status to change to Replicating.
-
Switch the source cluster to read-only.
-
On the transfer monitoring page, wait for the Maximum data transfer delay metric to decrease to zero. This means that all changes that occurred in the source cluster after data copying was completed are transferred to the target cluster.
-
Deactivate the transfer and wait for its status to change to Stopped.
For more information about transfer statuses, see Transfer lifecycle.
-
Transfer the load to the target cluster.
-
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need:
Manually created resourcesResources created with Terraform- Delete the Managed Service for PostgreSQL cluster.
- Delete the stopped transfer.
- Delete endpoints for both the source and target.
-
In the terminal window, go to the directory containing the infrastructure plan.
-
Delete the
data-transfer-pgsql-mpg.tf
configuration file. -
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
All the resources described in the
data-transfer-pgsql-mpg.tf
configuration file will be deleted. -
Transferring tables with data types from PostgreSQL extensions
Data Transfer allows you to copy tables, the columns of which contain data types defined in PostgreSQL and tables with derived types, i.e. arrays of these types and composite types with fields of these types. Currently, there is a limitation: the data type must implement binary I/O functions. This means that, for the type of data in the pg_typetypsend
and typreceive
column values must not be zero.
For example, for the PostGISGEOMETRY
GEOMETRY_DUMP
GEOGRAPHY
BOX2D
BOX3D
See also
For other migration methods, see the Yandex Managed Service for PostgreSQL documentation.