Migrate from Azure PostgreSQL to Neon
Learn how to migrate your database from Azure PostgreSQL to Neon using logical replication
This guide describes how to migrate your database from Azure Database for PostgreSQL to Neon, using logical replication.
Logical replication for Postgres transfers data from a source Postgres database to another, as a stream of tuples (records) or SQL statements. This allows for minimal downtime during the migration process, since all the records don't need to be copied at once.
Prerequisites
-
An Azure Database for PostgreSQL instance containing the data you want to migrate.
-
A Neon project to move the data to.
For detailed information on creating a Neon project, see Create a project. Make sure to create a project with the same Postgres version as your Azure PostgreSQL deployment.
-
Read the important notices about logical replication in Neon before you begin.
-
Review our logical replication tips, based on real-world customer data migration experiences.
Prepare your Azure PostgreSQL database
This section describes how to prepare your Azure PostgreSQL database (the publisher) for replicating data to your destination Neon database (the subscriber).
To illustrate the migration workflow, we set up the AdventureWorks sample database on an Azure Database for PostgreSQL deployment. This database contains data corresponding to a fictionaly bicycle parts company, organized across 5 schemas and almost 70 tables.
Enable logical replication in Azure PostgreSQL
-
Navigate to your Azure Database for PostgreSQL instance in the Azure portal.
-
From the left sidebar, select Server parameters under the Settings section.
-
Search for the
wal_level
parameter and set its value toLOGICAL
. -
Click Save to apply the changes.
note
Changing the
wal_level
parameter on Azure requires a server restart. This may cause a brief interruption to your database service.
Create a PostgreSQL role for replication
It is recommended that you create a dedicated Postgres role for replicating data. Connect to your Azure PostgreSQL database using a tool like psql or Azure Data Studio, then create a new role with REPLICATION
privileges:
Grant schema access to your PostgreSQL role
Grant the necessary permissions to your replication role. For example, the following commands grant access to all tables in the sales
schema to Postgres role replication_user
:
Granting SELECT ON ALL TABLES IN SCHEMA
instead of naming the specific tables avoids having to add privileges later if you add tables to your publication.
If you have data split across multiple schemas, you can run a similar command for each schema, or use a PL/pgSQL function to dynamically grant access to all schemas in the database.
Create a publication on the Azure PostgreSQL database
Publications define which tables will be replicated to the destination database. To create a publication for all tables in your database, run the following query:
This command creates a publication named azure_publication
that includes all tables in the public
schema, since we want to copy all the data. For details, see CREATE PUBLICATION, in the PostgreSQL documentation.
important
Avoid defining publications with FOR ALL TABLES
if you want the flexibility to add or drop tables from the publication later. It is not possible to modify a publication defined with FOR ALL TABLES
to include or exclude specific tables. For details, see Logical replication tips.
To create a publication for a specific table, you can use the following syntax:
To create a publication for multiple tables, provide a comma-separated list of tables:
For syntax details, see CREATE PUBLICATION, in the PostgreSQL documentation.
Allow inbound traffic from Neon
You need to allow inbound traffic from Neon Postgres servers so it can connect to your Azure database. To do this, follow these steps:
-
Log into the Azure portal and navigate to your Azure Postgres Server resource.
-
Click on the Networking option under the
Settings
section in the sidebar. Navigate to the Firewall Rules section under thePublic access
tab. -
Click on
Add a Firewall Rule
, which generates a modal to add the range of IP addresses from which we want to allow connections. You will need to perform this step for each of the NAT gateway IP addresses associated with your Neon project's region. For each IP address, create a new rule and fill both theStart IP
andEnd IP
fields with the IP address.Neon uses 3 to 6 IP addresses per region for this outbound communication, corresponding to each availability zone in the region. See NAT Gateway IP addresses for Neon's NAT gateway IP addresses.
-
To fetch the database schema using
pg_dump
, you also need to allow inbound traffic from your local machine (or where you are runningpg_dump
) so it can connect to your Azure database. Add another firewall rule entry with that IP address as the start and end IP address. -
CLick
Save
at the bottom to make sure all changes are saved.
Prepare your Neon destination database
This section describes how to prepare your destination Neon PostgreSQL database (the subscriber) to receive replicated data.
You can find the connection details for the Neon database on the Connection Details widget in the Neon Console. For details, see Connect from any application.
Create the Neon database
To keep parity with the Azure PostgreSQL deployment, create a new database with the same name. See Create a database for more information.
For this example, we run the following query to create a new database named AdventureWorks
in the Neon project.
Import the database schema
To ensure that the Neon AdventureWorks
database has the same schema as the Azure PostgreSQL database, we'll need to import the schema. You can use the pg_dump
utility to export the schema and then psql
to import it into Neon.
-
Export the schema from Azure PostgreSQL:
-
Import the schema into your Neon database:
Create a subscription
After importing the schema, create a subscription on the Neon database:
-
Use the Neon SQL Editor, psql, or another SQL client to connect to your Neon database.
-
Create the subscription using the
CREATE SUBSCRIPTION
statement: -
Verify that the subscription was created by running the following query, and confirming that the subscription (
neon_subscription
) is listed:
Monitor and verify the replication
To ensure that data is being replicated correctly:
-
Monitor the replication status on Neon, by running the following query:
This query should return an output similar to the following:
- An active
pid
indicates that the subscription is active and running. - The
received_lsn
andlatest_end_lsn
columns show the LSN (Log Sequence Number) of the last received (at Neon) and last written data (at Azure source), respectively. - In this example, they have the same value, which means that all the data has been successfully replicated from Azure to Neon.
- An active
-
To verify that the data has been replicated correctly, compare row counts between Azure PostgreSQL and Neon for some key tables. For example, you can run the following query to check the number of rows in the
addresses
table:It returns the same output on both databases:
-
Optionally, you can run some queries from your application against the Neon database to verify that it returns the same output as the Azure instance.
Complete the migration
Once the initial data sync is complete and you've verified that ongoing changes are being replicated:
- Stop writes to your Azure PostgreSQL database.
- Wait for any final transactions to be replicated to Neon.
- Update your application's connection string to point to your Neon database.
This ensures a much shorter downtime for the application, as you only need to wait for the last few transactions to be replicated before switching the application over to the Neon database.
note
Remember to update any Azure-specific configurations or extensions in your application code to be compatible with Neon. For Neon Postgres parameter settings, see Postgres parameter settings. For Postgres extensions supported by Neon, see Supported Postgres extensions.
Clean up
After successfully migrating and verifying your data on Neon, you can:
-
Drop the subscription on the Neon database:
-
Remove the publication from the Azure PostgreSQL database:
-
Consider backing up your Azure PostgreSQL database before decommissioning it.
Other migration options
This section discusses migration options other than using logical replication.
-
pg_dump and pg_restore
If your database size is not large, you can use the
pg_dump
utility to create a dump file of your database, and then usepg_restore
to restore the dump file to Neon. Please refer to the Migrate from Postgres guide for more information on this method. -
Postgres GUI clients
Some Postgres clients offer backup and restore capabilities. These include pgAdmin and phppgadmin, among others. We have not tested migrations using these clients, but if you are uncomfortable using command-line utilities, they may provide an alternative.
-
Table-level data migration using CSV files
Table-level data migration (using CSV files, for example) does not preserve database schemas, constraints, indexes, types, or other database features. You will have to create these separately. Table-level migration is simple but could result in significant downtime depending on the size of your data and the number of tables. For instructions, see Import data from CSV.
Reference
For more information about logical replication and Postgres client utilities, refer to the following topics in the Postgres and Neon documentation:
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.