|
| 1 | +# How to set up clusters for logical replication |
| 2 | + |
| 3 | +```{caution} |
| 4 | +This feature is only available for revision 630 or higher, which is not yet in the stable track. |
| 5 | +``` |
| 6 | + |
| 7 | +Start by deploying two PostgreSQL clusters: |
| 8 | +```sh |
| 9 | +juju deploy postgresql-k8s --channel 16/edge --trust postgresql1 |
| 10 | +juju deploy postgresql-k8s --channel 16/edge --trust postgresql2 |
| 11 | +``` |
| 12 | + |
| 13 | +For testing purposes, you can deploy two applications of the [data integrator charm](https://charmhub.io/data-integrator) and then integrate them to the two PostgreSQL clusters you want to replicate data between. |
| 14 | +```sh |
| 15 | +juju deploy data-integrator di1 --config database-name=testdb |
| 16 | +juju deploy data-integrator di2 --config database-name=testdb |
| 17 | + |
| 18 | +juju integrate postgresql1 di1 |
| 19 | +juju integrate postgresql2 di2 |
| 20 | +``` |
| 21 | + |
| 22 | +Then, integrate both PostgreSQL clusters: |
| 23 | +```sh |
| 24 | +juju integrate postgresql1:logical-replication-offer postgresql2:logical-replication |
| 25 | +``` |
| 26 | + |
| 27 | +This will create a publication on the first cluster and a subscription on the second cluster, allowing data to be replicated from the first to the second. |
| 28 | + |
| 29 | +Request the credentials for the first PostgreSQL cluster. |
| 30 | +```sh |
| 31 | +juju run di1/leader get-credentials |
| 32 | +``` |
| 33 | + |
| 34 | +The output example: |
| 35 | +```yaml |
| 36 | +postgresql: |
| 37 | + data: '{"database": "testdb", "external-node-connectivity": "true", "provided-secrets": |
| 38 | + "[\"mtls-cert\"]", "requested-secrets": "[\"username\", \"password\", \"tls\", |
| 39 | + \"tls-ca\", \"uris\", \"read-only-uris\"]"}' |
| 40 | + database: testdb |
| 41 | + endpoints: postgresql1-primary.dev.svc.cluster.local:5432 |
| 42 | + password: NTgtJkVfUHLiYDk5 |
| 43 | + read-only-endpoints: postgresql1-primary.dev.svc.cluster.local:5432 |
| 44 | + read-only-uris: postgresql://relation_id_8:[email protected]:5432/testdb |
| 45 | + tls: "False" |
| 46 | + tls-ca: "" |
| 47 | + uris: postgresql://relation_id_8:[email protected]:5432/testdb |
| 48 | + username: relation_id_8 |
| 49 | + version: "16.9" |
| 50 | +``` |
| 51 | +
|
| 52 | +Then create a table and insert some data into it on the first cluster: |
| 53 | +```sh |
| 54 | +psql postgresql://relation_id_8:[email protected]:5432/testdb |
| 55 | +psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1)) |
| 56 | +Type "help" for help. |
| 57 | + |
| 58 | +testdb=> create table asd (message int); insert into asd values (123); |
| 59 | +CREATE TABLE |
| 60 | +INSERT 0 1 |
| 61 | +``` |
| 62 | + |
| 63 | +After that, you need to create the same table on the second cluster so that the data can be replicated. Start by getting the credentials for the second cluster: |
| 64 | +```sh |
| 65 | +juju run di2/leader get-credentials |
| 66 | +``` |
| 67 | + |
| 68 | +The output example: |
| 69 | +```yaml |
| 70 | +postgresql: |
| 71 | + data: '{"database": "testdb", "external-node-connectivity": "true", "provided-secrets": |
| 72 | + "[\"mtls-cert\"]", "requested-secrets": "[\"username\", \"password\", \"tls\", |
| 73 | + \"tls-ca\", \"uris\", \"read-only-uris\"]"}' |
| 74 | + database: testdb |
| 75 | + endpoints: postgresql2-primary.dev.svc.cluster.local:5432 |
| 76 | + password: nfWkAiEtSA3iA7t2 |
| 77 | + read-only-endpoints: postgresql2-primary.dev.svc.cluster.local:5432 |
| 78 | + read-only-uris: postgresql://relation_id_9:[email protected]:5432/testdb |
| 79 | + tls: "False" |
| 80 | + tls-ca: "" |
| 81 | + uris: postgresql://relation_id_9:[email protected]:5432/testdb |
| 82 | + username: relation_id_9 |
| 83 | + version: "16.9" |
| 84 | +``` |
| 85 | +
|
| 86 | +Then create the same table on the second cluster: |
| 87 | +```sh |
| 88 | +psql postgresql://relation_id_9:[email protected]:5432/testdb |
| 89 | +psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1)) |
| 90 | +Type "help" for help. |
| 91 | + |
| 92 | +testdb=> create table asd (message int); |
| 93 | +CREATE TABLE |
| 94 | +``` |
| 95 | + |
| 96 | +Configure the replication of that specific database and table (remember to specify the table schema; it's the `public` schema in this example): |
| 97 | +```sh |
| 98 | +juju config postgresql2 logical_replication_subscription_request='{"testdb": ["public.asd"]}' |
| 99 | +``` |
| 100 | + |
| 101 | +After a few seconds, you can check that the data has been replicated: |
| 102 | +```sh |
| 103 | +psql postgresql://relation_id_9: [email protected]:5432/testdb |
| 104 | +psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1)) |
| 105 | +Type "help" for help. |
| 106 | + |
| 107 | +testdb=> select * from asd; |
| 108 | + message |
| 109 | +--------- |
| 110 | + 123 |
| 111 | +(1 row) |
| 112 | +``` |
| 113 | +
|
| 114 | +You can then add more data to the table in the first cluster, and it will be replicated to the second cluster automatically. |
| 115 | +
|
| 116 | +It's also possible to replicate tables in the other direction, from the second cluster to the first, while keeping the replication from the first cluster to the second. To do that, you need to also integrate the clusters in the opposite direction: |
| 117 | +```sh |
| 118 | +psql postgresql://relation_id_9:[email protected]:5432/testdb |
| 119 | +psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1)) |
| 120 | +Type "help" for help. |
| 121 | +
|
| 122 | +testdb=> create table asd2 (message int); insert into asd2 values (123); |
| 123 | +CREATE TABLE |
| 124 | +INSERT 0 1 |
| 125 | +testdb=> \q |
| 126 | +
|
| 127 | +psql postgresql://relation_id_8:[email protected]:5432/testdb |
| 128 | +psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1)) |
| 129 | +Type "help" for help. |
| 130 | +
|
| 131 | +testdb=> create table asd2 (message int); |
| 132 | +CREATE TABLE |
| 133 | +testdb=> \q |
| 134 | +
|
| 135 | +juju integrate postgresql1:logical-replication postgresql2:logical-replication-offer |
| 136 | +
|
| 137 | +juju config postgresql1 logical_replication_subscription_request='{"testdb": ["public.asd2"]}' |
| 138 | +
|
| 139 | +psql postgresql://relation_id_8:[email protected]:5432/testdb |
| 140 | +psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1)) |
| 141 | +Type "help" for help. |
| 142 | +
|
| 143 | +testdb=> select * from asd2; |
| 144 | + message |
| 145 | +--------- |
| 146 | + 123 |
| 147 | +(1 row) |
| 148 | +``` |
| 149 | +
|
| 150 | +And the same table, or even different tables, can be replicated to multiple clusters at the same time. For example, you can replicate the `asd` table from the first cluster to both a second and a third clusters, or you can replicate it only to the second cluster and replicate a different table to the third cluster. |
| 151 | +
|
| 152 | +If the relation between the PostgreSQL clusters is broken, the data will be kept in both clusters, but the replication will stop. You can re-enable logical replication by following the steps from [](/how-to/logical-replication/re-enable). |
| 153 | +
|
| 154 | +The same will happen for that specific table if you change the table in the `logical_replication_subscription_request` config option to a different table or remove it completely. If one or more tables other than the current one are specified, the replication will continue for those tables, but the current table will not be replicated any more. |
0 commit comments