List table changes with logical decoding
In this exercise you’ll configure logical replication, which is native to PostgreSQL. You will create two servers, which act as publisher and subscriber. Data in the zoodb will be replicated between them.
Before you start
You need your own Azure subscription to complete this exercise. If you don’t have an Azure subscription, you can create an Azure free trial.
Create resource group
- Sign in to the Azure portal. Your user account must be an Owner or Contributor for the Azure subscription.
- Select Resource groups and then select + Create.
- Select your subscription.
- In Resource group, enter rg-PostgreSQL_Replication.
- Select a region close to your location.
- Select Review + create.
- Select Create.
Create a publisher server
- Under Azure services, select + Create a resource. Under Categories, select Databases. Under Azure Database for PostgreSQL, select Create.
- On the Flexible server Basics tab, enter each field as follows:
- Subscription - your subscription.
- Resource group - select rg-PostgreSQL_Replication.
- Server name - psql-postgresql-pub9999 (Name must be globally unique, so replace 9999 with four random numbers).
- Region - select the same region as the resource group.
- PostgreSQL version - select 16.
- Workload type - Development.
- Compute + storage - Burstable. Select Configure server and examine the configuration options. Don’t make any changes and close the section.
- Availability zone - 1. If availability zones aren’t supported, leave as No preference.
- High availability - Disabled.
- Authentication method - PostgreSQL authentication only.
- In admin username, enter
demo
. - In password, enter a suitable complex password.
- Select Next: Networking >.
- On the Flexible server Networking tab, enter each field as follows:
- Connectivity method: (o) Public access (allowed IP addresses).
- Allow public access from any Azure service within Azure to this server - checked. This must be checked, so that the publisher and subscriber databases can communicate with each other.
- Under Firewall rules, select + Add current client IP address. This adds your current IP address as a firewall rule. You can optionally name this firewall rule to something meaningful.
- Select Review + create. Then select Create.
- Since creating an Azure Database for PostgreSQL can take a few minutes, start with the next step as soon this deployment is in progress. Remember to open a new browser window or tab to continue.
Create a subscriber server
- Under Azure services, select + Create a resource. Under Categories, select Databases. Under Azure Database for PostgreSQL, select Create.
- On the Flexible server Basics tab, enter each field as follows:
- Subscription - your subscription.
- Resource group - select rg-PostgreSQL_Replication.
- Server name - psql-postgresql-sub9999 (Name must be globally unique, so replace 9999 with four random numbers).
- Region - select the same region as the resource group.
- PostgreSQL version - select 16.
- Workload type - Development.
- Compute + storage - Burstable. Select Configure server and examine the configuration options. Don’t make any changes and close the section.
- Availability zone - 2. If availability zones aren’t supported, leave as No preference.
- High availability - Disabled.
- Authentication method - PostgreSQL authentication only.
- In admin username, enter
demo
. - In password, enter a suitable complex password.
- Select Next: Networking >.
- On the Flexible server Networking tab, enter each field as follows:
- Connectivity method: (o) Public access (allowed IP addresses)
- Allow public access from any Azure service within Azure to this server - checked. This must be checked, so that the publisher and subscriber databases can communicate with each other.
- Under Firewall rules, select + Add current client IP address. This adds your current IP address as a firewall rule. You can optionally name this firewall rule to something meaningful.
- Select Review + create. Then select Create.
- Wait for both Azure Database for PostgreSQL servers to be deployed.
Set up replication
For both the publisher and subscriber servers:
- In the Azure portal, navigate to the server and under Settings select Server parameters.
- Using the search bar, find each parameter and make the following changes:
wal_level
= LOGICALmax_worker_processes
= 24
- Select Save. Then select Save and Restart.
-
Wait for both servers to restart.
Note
After the servers are re-deployed, you might have to refresh your browser windows to notice that the servers have restarted.
Before you continue
Make sure you have:
- You have installed and started both Azure Database for PostgreSQL flexible servers.
- You have already cloned the lab scripts from PostgreSQL Labs. If you haven’t done so, clone the repository locally:
- Open a command line/terminal.
- Run the command:
md .\DP3021Lab git clone https://github.com/MicrosoftLearning/mslearn-postgresql.git .\DP3021Lab
NOTE
If git is not installed, download and install the git app and try running the previous commands again.
- You have installed Azure Data Studio. If you haven’t then download and install Azure Data Studio.
- Install the PostgreSQL extension in Azure Data Studio.
Set up the publisher
- Open Azure Data Studio and connect to the publisher server. (Copy the server name from the Overview section.)
- Select File, Open file and navigate to the folder where you saved the scripts.
- Open the script ../Allfiles/Labs/06/Lab6_Replication.sql and connect to the server.
- Highlight and run the section Grant the admin user replication permission.
- Highlight and run the section Create zoodb database.
- Select zoodb as the current database using the dropdown list on the toolbar. Verify that zoodb is the current database by running the SELECT statement.
- Highlight and run the section Create tables and foreign key constraints in zoodb.
- Highlight and run the section Populate the tables in zoodb.
- Highlight and run the section Create a publication. When you run the SELECT statement it will not list anything, because the replication isn’t yet active.
Set up the subscriber
- Open a second instance of Azure Data Studio and connect to the subscriber server.
- Select File, Open file and navigate to the folder where you saved the scripts.
- Open the script ../Allfiles/Labs/06/Lab6_Replication.sql and connect to the subscriber server. (Copy the server name from the Overview section.)
- Highlight and run the section Grant the admin user replication permission.
- Highlight and run the section Create zoodb database.
- Select zoodb as the current database using the dropdown list on the toolbar. Verify that zoodb is the current database by running the SELECT statement.
- Highlight and run the section Create tables and foreign key constraints in zoodb.
- Scroll down to the section Create a subscription.
- Edit the CREATE SUBSCRIPTION statement so that it has the correct publisher server name and the publisher’s strong password. Highlight and run the statement.
- Highlight and run the SELECT statement. This shows the subscription “sub” that you’ve created.
- Under the section Display the tables, highlight and run each SELECT statement. The tables have been populated by replication from the publisher.
Make changes to the publisher database
- In the first instance of Azure Data Studio (your publisher instance), under Insert more animals highlight and run the INSERT statement. Make sure you don’t run this INSERT statement at the subscriber.
View the changes in the subscriber database
- In the second instance of Azure Data Studio (subscriber), under Display the animal tables highlight and run the SELECT statement.
You’ve now created two Azure Database for PostgreSQL flexible servers and configured one as a publisher, and the other as a subscriber. In the publisher database you created and populated the zoo database. In the subscriber database, you created an empty database, which was then populated by streaming replication.
Clean-Up
- After you’ve finished the exercise, delete the resource group containing both servers. You’ll be charged for the servers unless you either STOP or DELETE them.
- If needed, delete the .\DP3021Lab folder.