Replicate data from Amazon RDS Postgres
newLearn how to replicate data from Amazon RDS Postgres to Neon
Neon's logical replication feature allows you to replicate data from Amazon RDS PostgreSQL to Neon.
Prerequisites
-
A source database in Amazon RDS for PostgreSQL containing the data you want to replicate. If you're just testing this out and need some data to play with, you can use the following statements to create a table with sample data:
-
A destination Neon project. For information about creating a Neon project, see Create a project.
-
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 source database
This section describes how to prepare your source Amazon RDS Postgres instance (the publisher) for replicating data to Neon.
Enable logical replication in the source Amazon RDS PostgreSQL instance
Enabling logical replication in Postgres requires changing the wal_level
configuration parameter from replica
to logical
. Before you begin, you can check your current setting with the following command:
note
For information about connecting to RDS from psql
, see Connect to a PostgreSQL DB instance.
If your current setting is replica
, follow these steps to enable logical replication. If you are using the default parameter group, you will need to create a new parameter group to set the value. You can do so by selecting Parameter groups > Create parameter group from the sidebar and filling in the required fields.
To enable logical replication:
-
Navigate to the Configuration tab of your RDS instance.
-
Under the Configuration heading, click on the DB instance parameter group link.
-
Click Edit. In the Filter parameters search field, search for
rds.logical_replication
. -
Set the value to
1
, and click Save Changes. -
If you created a new parameter group, navigate back to your RDS instance page, click Modify, and scroll down to select your new parameter group. Click Continue, and select Apply immediately to make the change now, then click Modify DB instance.
-
Reboot your instance to apply the new setting. From the Actions menu for your database, select Reboot.
-
Make sure that the
wal_level
parameter is now set tological
:
Allow connections from Neon
You need to allow inbound connections to your AWS RDS Postgres instance from Neon. You can do this by editing your instance's CIDR/IP - Inbound security group, which you can find a link to from your AWS RDS Postgres instance page.
-
Click on the security group name.
-
Click on the security group ID.
-
From the Actions menu, select Edit inbound rules.
-
Add rules that allow traffic from each of the IP addresses for your Neon project's region.
Neon uses 3 to 6 IP addresses per region for outbound communication, corresponding to each availability zone in the region. See NAT Gateway IP addresses for Neon's NAT gateway IP addresses.
-
When you're finished, click Save rules.
note
You can specify a rule for
0.0.0.0/0
to allow traffic from any IP address. However, this configuration is not considered secure.
Create a publication on the source database
Publications are a fundamental part of logical replication in Postgres. They define what will be replicated.
To create a publication for all tables in your source database, run the following query. You can use a publication name of your choice.
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.
Prepare your destination database
This section describes how to prepare your source Neon Postgres database (the subscriber) to receive replicated data from your AWS RDS Postgres instance.
Prepare your database schema
When configuring logical replication in Postgres, the tables in the source database you are replicating from must also exist in the destination database, and they must have the same table names and columns. You can create the tables manually in your destination database or use utilities like pg_dump
and pg_restore
to dump the schema from your source database and load it to your destination database. See Import a database schema for instructions.
If you're using the sample playing_with_neon
table, you can create the same table on the destination database with the following statement:
Create a subscription
After creating a publication on the source database, you need to create a subscription on your Neon destination database.
-
Use the Neon SQL Editor,
psql
, or another SQL client to connect to your destination database. -
Create the subscription using the using a
CREATE SUBSCRIPTION
statement.subscription_name
: A name you chose for the subscription.connection_string
: The connection string for the source AWS RDS Postgres database where you defined the publication.publication_name
: The name of the publication you created on the source AWS RDS Postgres database.
-
Verify the subscription was created by running the following command:
The subscription (
my_subscription
) should be listed, confirming that your subscription was created.
Test the replication
Testing your logical replication setup ensures that data is being replicated correctly from the publisher to the subscriber database.
-
Run some data modifying queries on the source database (inserts, updates, or deletes). If you're using the
playing_with_neon
database, you can use this statement to insert some rows: -
Perform a row count on the source and destination databases to make sure the result matches.
Alternatively, you can run the following query on the subscriber to make sure the last_msg_receipt_time
is as expected. For example, if you just ran an insert option on the publisher, the last_msg_receipt_time
should reflect the time of that operation.
Switch over your application
After the replication operation is complete, you can switch your application over to the destination database by swapping out your AWS RDS source database connection details for your Neon destination database connection details.
You can find your Neon connection details on the Connection Details widget in the Neon Console. For details, see Connect from any application.