Migrate a database to Cloud SQL for PostgreSQL by using Database Migration Service

MySQL  |  PostgreSQL  |  PostgreSQL to AlloyDB


This quickstart shows you how to use Database Migration Service to migrate data to Cloud SQL for PostgreSQL. The resources created in this quickstart typically cost less than one dollar (USD), assuming you complete the steps, including the clean up, in a timely manner.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Database Migration Service API.

    Enable the API

  5. Make sure that you have the Database Migration Admin role assigned to your user account.

    Go to the IAM page

Requirements

Database Migration Service offers a variety of source database options andconnectivity methods. Different sources work better with some connectivitymethods than with others. In this quickstart, we assume that you're using astandalone PostgreSQL database in an environment where you can configure your networkto add an inbound firewall rule. The source database can be on-premises or in acloud provider. Because we can't know the specifics of your environment, wecan't provide detailed steps when it comes to your networking configuration.

In this quickstart, you'll selectPostgreSQL for theSource database engine andIPallowlist as the networking method.

Create a source connection profile

By creating a connection profile, you're creating a record that contains informationabout the source database. Database Migration Service uses the information in theconnection profile to migrate data from your source database to the destinationCloud SQL database instance.
  1. Go to the Database Migration Service Connection profiles page inthe Google Cloud console.

    Go to the Database Migration Service Connection Profiles Page

  2. ClickCREATE PROFILE.

  3. On theCreate a connection profile page, from theProfile role list, selectSource.

  4. From theDatabase engine list, select one of the following classification types of your source database:

    • A standalone PostgreSQL instance (PostgreSQL)
    • An existing Cloud SQL for PostgreSQL instance (Cloud SQL for PostgreSQL)
    • An Amazon RDS instance for PostgreSQL (Amazon RDS for PostgreSQL)

    For this quickstart, selectPostgreSQL.

  1. Supply the following information:
    • In theConnection profile name field, enter a name for the connection profile for your source database, such asMy Connection Profile.
    • Keep the auto-generatedConnection profile ID.
    • EnterConnectivity information:
      • If you're replicating from a self-hosted database, then enter theHostname or IP address (domain or IP) andPort to access the host. (The default PostgreSQL port is 5432.)
      • If you're replicating from a Cloud SQL database, then select the Cloud SQL instance from the drop-down list.
    • Enter aUsername andPassword to authenticate to your source instance.
    • (Optional) If you plan to transfer sensitive information over a public network (by using IP allowlists), then we recommend usingSSL/TLS encryption for the connection between the source and destination databases. Otherwise, keep the default value ofNone.
  2. In theConnection profile region section of the page, select the region where you want to save the connection profile.

    Connection profiles, like all resources, are saved in a region. Region selection doesn't impact which migration jobs can use them, or which regions can connect to the data location itself, but can impact availability in the case of regional downtime.

  3. ClickCREATE.

Create a migration job

Database Migration Service uses migration jobs to migrate data from your source database instance to the destination Cloud SQL database instance.

Creating a migration job includes:

  • Defining settings for the job
  • Selecting the connection profile that you created for your source database (source connection profile)
  • Defining settings for the destination Cloud SQL database instance
  • Setting up connectivity between the source and destination database instances
  • Testing the migration job to ensure that the connection information you provided for the job is valid

Define settings for the migration job

  1. Go to the Database Migration Service Migration jobs page inthe Google Cloud console.

    Go to the Database Migration Service Migration Jobs Page

  2. ClickCREATE MIGRATION JOB.

  3. In theMigration job name field, enter a name for the migration job, such asMy Migration Job.

  4. Keep the auto-generatedMigration job ID.

  5. Open theSource database engine drop-down list and select the classification type of your sourcedatabase.

  6. Select theDestination region where the destination instance is to be created.

  7. Set theMigration job type to "Continuous" because you want ongoing changes in your source database to be migrated to the destination Cloud SQL database instance.

  8. Review the required prerequisites that are generated automatically to reflect how the environment must be prepared for a migration job. These prerequisites can include how to configure the source database and how to connect it to the destination Cloud SQL database instance. It's best to complete these prerequisites at this step, but you can complete them at any time before you test the migration job or start it. For more information about these prerequisites, seeConfigure your source.

  9. ClickSAVE & CONTINUE.

Specify information about the source connection profile

  1. Open theSelect source connection profile drop-down list and select the connection profile that you created.

  2. ClickSAVE & CONTINUE.

Define settings for the destination Cloud SQL instance

  1. Provide aDestination Instance ID for the Cloud SQL instance. By default, themigration job ID is pre-populated in this field. You can keep this name or enter anothername.

  2. Provide an alphanumeric password for the destination Cloud SQL instance. This will be the password for thepostgres administrator account in the instance.

    You can either enter the password manually or clickGENERATE to have Database Migration Service create one for you automatically.

    Tip: To see the password in clear text, click theReveal icon. This icon looks like an eye.
  3. Choose theDatabase version for the destination instance from the listof supported Cloud SQL versions for the specified database engine.

  4. Select the desired Google CloudZone for the newCloud SQL instance (or leave as "Any").

  5. Select thePublic IP check box because, for this quickstart, you're using IP allowlist as the networking method. This method works by configuring the source database server to accept connections from the outgoing IP address of the Cloud SQL instance.

    For othernetworking methods, such as reverse SSH tunnel, VPC peering, or VPN, select thePrivate IP check box.

  6. Select theMachine type for the Cloud SQL instance. The disk sizemust be equal to or greater than the source database size.

  7. Define theStorage type andStorage capacity for the Cloud SQLinstance.

  8. ClickCREATE & CONTINUE to create the new instance.

  9. ClickCREATE DESTINATION & CONTINUE in the subsequent dialog box.Creating the instance may take several minutes to complete.

    Important: This procedure creates a Cloud SQL instance. You arecharged according to the configuration that you select.Note: After the destination instance is created, some settings of themigration job can no longer be edited. You can't change thedatabase engine or migration type.

Set up connectivity

  1. Choose the networking method that you'd like to use to establish connectivity between the source and destination databases. For this quickstart, use theConnectivity method list to selectIP allowlist as the networking method.

    IP allowlist is a security feature often used for limiting and controlling access to the data in your source and destination databases only to trusted users. You can use IP allowlists to create lists of trusted IP addresses or IP ranges from which your users can access this data. Also, to use IP allowlists, you must open the source database or firewall to incoming connections from the destination Cloud SQL instance.

    It may take a few minutes for an IP address to appear in theDestination outgoing IP address field.

  2. If the source IP address isn't pre-populated in this field, then copy the Cloud SQL instance's outgoing IP address from the console, and use it to configure the network firewall for the source database server to accept connections from this IP address.
    1. Update thepg_hba.conf file or Amazon RDS security groups to accept connections from this IP address.
    2. In your firewall rules, create an Inbound (or Ingress) rule to allow connections on port 5432 from the destination Cloud SQL instance's connectivity IP address.
  3. ClickCONFIGURE & CONTINUE to complete the connection profile configuration.

Configure migration databases

You can select the databases that you want to migrate.

  1. From theDatabases to migrate list, selectAll databases.

    All databases that exist on the source are selected for migration.

  2. ClickSave and continue.

Test and create the migration job

  1. Review the settings you chose for the migration job.

  2. ClickTEST JOB to verify that the source has been configuredcorrectly, that the source and destination instances are able to communicatewith each other, and that the migration job is valid.

  3. Verify that you see the "Tests passed successfully!" status.

    If the test fails, then you can address the problem in the appropriate part ofthe flow, and return to re-test.

  4. ClickCREATE & START JOB to create the migration job and start itimmediately.

  5. ClickSTART in the subsequent dialog box.

  6. In theMigration jobs page, verify that your migration job has a status of "Starting". After a few minutes, confirm that the status changes to "Running".

Verify the migration job

In this section, you confirm that Database Migration Service used the migration job to migrate data from your source database instance to the destination Cloud SQL database instance.

  1. Go to theSQL Instances page in the Google Cloud console.
  2. Click the read replica entry of your migration job.
  3. Click theActivate Cloud Shell icon that appears in the upper-right region of the page.
  4. At the Google Cloud Shell prompt, pressEnter.
  5. Optional: If anAuthorize Cloud Shell dialog box appears, then clickAuthorize.
  6. Connect to the destination Cloud SQL database instance.
  7. At theEnter password prompt, enter the password that you either provided or that Database Migration Service generated for you inDefine settings for the destination Cloud SQL instance.

  1. At thepostgres prompt, enter\list to list the databases and verify that you see your source database instance.
  2. At thepostgres prompt, enter\connectSOURCE_DB_NAME because you want to see the tables associated with this database instance. The name of the prompt changes frompostgres toSOURCE_DB_NAME.
  3. At theSOURCE_DB_NAME prompt, enter\dt to see the tables of this instance.
  4. At theSOURCE_DB_NAME prompt, enterGRANT cloudsqlexternalsync toUSER; because you want to give this user permission to access the data in the tables of this instance.

    ReplaceUSER with the name of the user you used to connect to the destination database instance.

  5. At theSOURCE_DB_NAME prompt, enterSELECT * fromTABLE_NAME; to see the information that's replicated from a table in your source database instance.
  6. Verify that you see the correct information in the table.

This confirms that Database Migration Service migrated the data.

Promote the migration job

You're ready to promote the migration job. As a result, the destinationCloud SQL database instance replaces the source database instance as the primary database.

  1. Return to theMigration jobs page.

  2. Click the migration job that you want to promote. TheMigration job details page appears.

  3. Wait for the replication delay to trend toward zero.

  4. Stop all writes to the source database.

    You must stop all writes to the source database because, by promoting the migration job, the destination Cloud SQL database will become your primary database.
  5. Wait until the replication delay is at zero.

  6. Click the migration job that you want to promote. This job should have a status of "Running".

  7. ClickPROMOTE to promote the migration job.

  8. ClickPROMOTE again in the subsequent dialog box.

  9. Verify that the migration job has a status of "Promote in progress". After a few minutes, confirm that the status changes to "Completed".

Your new Cloud SQL database instance is ready to use.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. Use the Google Cloud consoleto delete your migration job, connection profile, Cloud SQL instance, andproject if you don't need them.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-09 UTC.