Tutorial: Migrate RDS PostgreSQL to Azure Database for PostgreSQL online using DMS
Important
We recommend that you use the new migration service in Azure Database for PostgreSQL for a more streamlined and efficient migration experience. This service simplifies the process by supporting a variety of source environments, ensuring a hassle-free transition to Azure Database for PostgreSQL.
You can use Azure Database Migration Service to migrate databases from an RDS PostgreSQL instance to Azure Database for PostgreSQL while the source database remains online during migration. In other words, migration can be achieved with minimal downtime to the application. In this tutorial, you migrate the DVD Rental
sample database from an instance of RDS PostgreSQL 9.6 to Azure Database for PostgreSQL by using the online migration activity in Azure Database Migration Service.
In this tutorial, you learn how to:
- Migrate the sample schema by using the pg_dump utility.
- Create an instance of Azure Database Migration Service.
- Create a migration project by using Azure Database Migration Service.
- Run the migration.
- Monitor the migration.
- Perform migration cutover.
Using the Azure Database Migration Service to perform an online migration requires creating an instance based on the Premium pricing tier. For more information, see the Azure Database Migration Service pricing page. We encrypt disk to prevent data theft during the process of migration.
Important
For an optimal migration experience, Microsoft recommends creating an instance of the Azure Database Migration Service in the same Azure region as the target database. Moving data across regions or geographies can slow down the migration process and introduce errors.
Tip
In Azure Database Migration Service, you can migrate your databases offline or while they are online. In an offline migration, application downtime starts when the migration starts. To limit downtime to the time it takes you to cut over to the new environment after the migration, use an online migration. We recommend that you test an offline migration to determine whether the downtime is acceptable. If the expected downtime isn't acceptable, do an online migration.
This article describes how to perform an online migration from an on-premises instance of PostgreSQL to Azure Database for PostgreSQL.
Prerequisites
To complete this tutorial, you need to:
Download and install PostgreSQL community edition 9.5, 9.6, or 10. The source PostgreSQL Server version must be 9.5.11, 9.6.7, 10, or later. For more information, see the article Supported PostgreSQL Database Versions.
The target Azure Database for PostgreSQL version must be equal to or later than the RDS PostgreSQL version. For example, RDS PostgreSQL 9.6 can only migrate to Azure Database for PostgreSQL 9.6, 10, or 11, but not to Azure Database for PostgreSQL 9.5.
Create an instance of Azure Database for PostgreSQL - Flexible Server.
Create a Microsoft Azure Virtual Network for Azure Database Migration Service by using the Azure Resource Manager deployment model, which provides site-to-site connectivity to your on-premises source servers by using either ExpressRoute or VPN. For more information about creating a virtual network, see the Virtual Network Documentation, and especially the quickstart articles with step-by-step details.
Ensure that your virtual network Network Security Group rules don't block the outbound port 443 of ServiceTag for ServiceBus, Storage, and AzureMonitor. For more detail on virtual network NSG traffic filtering, see the article Filter network traffic with network security groups.
Configure your Windows Firewall for database engine access.
Open your Windows Firewall to allow Azure Database Migration Service to access the source PostgreSQL server, which by default is TCP port 5432.
When using a firewall appliance in front of your source databases, you might need to add firewall rules to allow the Azure Database Migration Service to access the source databases for migration.
Create a server-level firewall rule for the Azure Database for PostgreSQL server to allow Azure Database Migration Service access to the target databases. Provide the subnet range of the virtual network used for Azure Database Migration Service.
Set up AWS RDS PostgreSQL for replication
To create a new parameter group, follow the instructions provided by AWS in the article Working with DB Parameter Groups.
Use the master user name to connect to the source from Azure Database Migration Service. If you use an account other than the master user account, the account must have the rds_superuser role and the rds_replication role. The rds_replication role grants permissions to manage logical slots and to stream data using logical slots.
Create a new parameter group with the following configuration:
Set the
rds.logical_replication
parameter in your DB parameter group to1
.max_wal_senders
= [number of concurrent tasks]. Themax_wal_senders
parameter sets the number of concurrent tasks that can run. Recommended setting:10
.max_replication_slots
= [number of slots]. Recommended setting:5
.
Associate the parameter group you created to the RDS PostgreSQL instance.
Migrate the schema
Extract the schema from the source database and apply to the target database to complete migration of all database objects such as table schemas, indexes, and stored procedures.
The easiest way to migrate only the schema is to use pg_dump with the -s option. For more information, see the examples in the Postgres pg_dump tutorial.
pg_dump -O -h hostname -U db_username -d db_name -s > your_schema.sql
For example, to dump a schema file for the dvdrental database, use the following command:
pg_dump -O -h localhost -U postgres -d dvdrental -s > dvdrentalSchema.sql
Create an empty database in the target service, which is Azure Database for PostgreSQL- Flexible server.
Import the schema to target service, which is Azure Database for PostgreSQL. To restore the schema dump file, run the following command:
psql -h hostname -U db_username -d db_name < your_schema.sql
For example:
psql -h mypgserver-20170401.postgres.database.azure.com -U postgres -d dvdrental < dvdrentalSchema.sql
Note
The migration service internally handles the enable/disable of foreign keys and triggers to ensure a reliable and robust data migration. As a result, you don't have to worry about making any modifications to the target database schema.
Register the resource provider
Register the Microsoft.DataMigration resource provider before you create your first instance of the Database Migration Service.
Sign in to the Azure portal. Search for and select Subscriptions.
Select the subscription in which you want to create the instance of Azure Database Migration Service, and then select Resource providers.
Search for migration, and then select Register for Microsoft.DataMigration.
Create an instance of Azure Database Migration Service
In the Azure portal, select + Create a resource, search for Azure Database Migration Service, and then select Azure Database Migration Service from the dropdown list.
On the Azure Database Migration Service screen, select Create.
On the Create Migration Service screen, specify a name for the service, the subscription, and a new or existing resource group.
Select the location in which you want to create the instance of Azure Database Migration Service.
Select an existing virtual network or create a new one.
The virtual network provides Azure Database Migration Service with access to the source PostgreSQL instance and the target Azure Database for PostgreSQL instance.
For more information about how to create a virtual network in the Azure portal, see the article Create a virtual network using the Azure portal.
Select a pricing tier. For this online migration, select the Premium: 4vCores pricing tier.
Select Create to create the service.
Create a migration project
After the service is created, locate it within the Azure portal, open it, and then create a new migration project.
In the Azure portal, select All services, search for Azure Database Migration Service, and then select Azure Database Migration Services.
On the Azure Database Migration Services screen, search for the name of the Azure Database Migration Service instance that you created, select the instance, and then select + New Migration Project.
On the New migration project screen, specify a name for the project, in the Source server type text box, select AWS RDS for PostgreSQL, and then in the Target server type text box, select Azure Database for PostgreSQL.
In the Choose type of activity section, select Online data migration.
Important
Select Online data migration. Offline migrations aren't supported for this scenario.
Alternately, you can choose Create project only to create the migration project now and execute the migration later.
Select Save.
Select Create and run activity to create the project and run the migration activity.
Make a note of the prerequisites needed to set up online migration in the project creation pane.
Specify source details
On the Add Source Details screen, specify the connection details for the source PostgreSQL instance.
Specify target details
Select Save, and then on the Target details screen, specify the connection details for the target Azure Database for PostgreSQL server, which is pre-provisioned and has the DVD Rentals schema deployed using pg_dump.
Select Save, and then on the Map to target databases screen, map the source and the target database for migration.
If the target database contains the same database name as the source database, Azure Database Migration Service selects the target database by default.
Select Save, on the Migration summary screen, in the Activity name text box, specify a name for the migration activity, and then review the summary to ensure that the source and target details match what you previously specified.
Run the migration
Select Run migration.
The migration activity window appears, and the Status of the activity is Initializing.
Monitor the migration
On the migration activity screen, select Refresh to update the display until the Status of the migration shows as Running.
Under DATABASE NAME, select a specific database to get to the migration status for Full data load and Incremental data sync operations.
Full data load shows the initial load migration status, while Incremental data sync shows change data capture (CDC) status.
Perform migration cutover
After the initial Full load is completed, the databases are marked Ready to Cutover.
When you're ready to complete the database migration, select Start Cutover.
Wait until the Pending changes counter shows
0
to ensure that all incoming transactions to the source database are stopped, select the Confirm checkbox, and then select Apply.When the database migration status shows Completed, connect your applications to the new target Azure Database for PostgreSQL database.
Your online migration of an on-premises instance of RDS PostgreSQL to Azure Database for PostgreSQL is now complete.