|
| 1 | +--- |
| 2 | +title: "Logical Replication" |
| 3 | +date: |
| 4 | +draft: false |
| 5 | +weight: 150 |
| 6 | +--- |
| 7 | + |
| 8 | +[Logical replication](https://www.postgresql.org/docs/current/logical-replication.html) is a Postgres feature that provides a convenient way for moving data between databases, particularly Postgres clusters that are in an active state. |
| 9 | + |
| 10 | +You can set up your PGO managed Postgres clusters to use logical replication. This guide provides an example for how to do so. |
| 11 | + |
| 12 | +## Set Up Logical Replication |
| 13 | + |
| 14 | +This example creates two separate Postgres clusters named `hippo` and `rhino`. We will logically replicate data from `rhino` to `hippo`. We can create these two Postgres clusters using the manifests below: |
| 15 | + |
| 16 | +``` |
| 17 | +--- |
| 18 | +apiVersion: postgres-operator.crunchydata.com/v1beta1 |
| 19 | +kind: PostgresCluster |
| 20 | +metadata: |
| 21 | + name: hippo |
| 22 | +spec: |
| 23 | + image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:centos8-13.4-1 |
| 24 | + postgresVersion: 13 |
| 25 | + instances: |
| 26 | + - dataVolumeClaimSpec: |
| 27 | + accessModes: |
| 28 | + - "ReadWriteOnce" |
| 29 | + resources: |
| 30 | + requests: |
| 31 | + storage: 1Gi |
| 32 | + backups: |
| 33 | + pgbackrest: |
| 34 | + image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.35-0 |
| 35 | + repos: |
| 36 | + - name: repo1 |
| 37 | + volume: |
| 38 | + volumeClaimSpec: |
| 39 | + accessModes: |
| 40 | + - "ReadWriteOnce" |
| 41 | + resources: |
| 42 | + requests: |
| 43 | + storage: 1Gi |
| 44 | +--- |
| 45 | +apiVersion: postgres-operator.crunchydata.com/v1beta1 |
| 46 | +kind: PostgresCluster |
| 47 | +metadata: |
| 48 | + name: rhino |
| 49 | +spec: |
| 50 | + image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:centos8-13.4-1 |
| 51 | + postgresVersion: 13 |
| 52 | + instances: |
| 53 | + - dataVolumeClaimSpec: |
| 54 | + accessModes: |
| 55 | + - "ReadWriteOnce" |
| 56 | + resources: |
| 57 | + requests: |
| 58 | + storage: 1Gi |
| 59 | + backups: |
| 60 | + pgbackrest: |
| 61 | + image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.35-0 |
| 62 | + repos: |
| 63 | + - name: repo1 |
| 64 | + volume: |
| 65 | + volumeClaimSpec: |
| 66 | + accessModes: |
| 67 | + - "ReadWriteOnce" |
| 68 | + resources: |
| 69 | + requests: |
| 70 | + storage: 1Gi |
| 71 | + users: |
| 72 | + - name: logic |
| 73 | + databases: |
| 74 | + - zoo |
| 75 | + options: "REPLICATION" |
| 76 | +``` |
| 77 | + |
| 78 | +The key difference between the two Postgres clusters is this section in the `rhino` manifest: |
| 79 | + |
| 80 | +``` |
| 81 | +users: |
| 82 | + - name: logic |
| 83 | + databases: |
| 84 | + - zoo |
| 85 | + options: "REPLICATION" |
| 86 | +``` |
| 87 | + |
| 88 | +This creates a database called `zoo` and a user named `logic` with `REPLICATION` privileges. This will allow for replicating data logically to the `hippo` Postgres cluster. |
| 89 | + |
| 90 | +Create these two Postgres clusters. When the `rhino` cluster is ready, [log into the `zoo` database]({{< relref "tutorial/connect-cluster.md" >}}). For convenience, you can use the `kubectl exec` method of logging in: |
| 91 | + |
| 92 | +``` |
| 93 | +kubectl exec -it -n postgres-operator -c database \ |
| 94 | + $(kubectl get pods -n postgres-operator --selector='postgres-operator.crunchydata.com/cluster=rhino,postgres-operator.crunchydata.com/role=master' -o name) -- psql zoo |
| 95 | +``` |
| 96 | + |
| 97 | +Let's create a simple table called `abc` that contains just integer data. We will also populate this table: |
| 98 | + |
| 99 | +``` |
| 100 | +CREATE TABLE abc (id int PRIMARY KEY); |
| 101 | +INSERT INTO abc SELECT * FROM generate_series(1,10); |
| 102 | +``` |
| 103 | + |
| 104 | +We need to grant `SELECT` privileges to the `logic` user in order for it to perform an initial data synchronization during logical replication. You can do so with the following command: |
| 105 | + |
| 106 | +``` |
| 107 | +GRANT SELECT ON abc TO logic; |
| 108 | +``` |
| 109 | + |
| 110 | +Finally, create a [publication](https://www.postgresql.org/docs/current/logical-replication-publication.html) that allows for the replication of data from `abc`: |
| 111 | + |
| 112 | +``` |
| 113 | +CREATE PUBLICATION zoo FOR ALL TABLES; |
| 114 | +``` |
| 115 | + |
| 116 | +Quit out of the `rhino` Postgres cluster. |
| 117 | + |
| 118 | +For the next step, you will need to get the connection information for how to connection as the `logic` user to the `rhino` Postgres database. You can get the key information from the following commands, which return the hostname, username, and password: |
| 119 | + |
| 120 | +``` |
| 121 | +kubectl -n postgres-operator get secrets rhino-pguser-logic -o jsonpath={.data.host} | base64 -d |
| 122 | +kubectl -n postgres-operator get secrets rhino-pguser-logic -o jsonpath={.data.user} | base64 -d |
| 123 | +kubectl -n postgres-operator get secrets rhino-pguser-logic -o jsonpath={.data.password} | base64 -d |
| 124 | +``` |
| 125 | + |
| 126 | +The host will be something like `rhino-primary.postgres-operator.svc` and the user will be `logic`. Further down, the guide references the password as `<LOGIC-PASSWORD>`. You can substitute the actual password there. |
| 127 | + |
| 128 | +Log into the `hippo` Postgres cluster. Note that we are logging into the `postgres` database within the `hippo` cluster: |
| 129 | + |
| 130 | +``` |
| 131 | +kubectl exec -it -n postgres-operator -c database \ |
| 132 | + $(kubectl get pods -n postgres-operator --selector='postgres-operator.crunchydata.com/cluster=hippo,postgres-operator.crunchydata.com/role=master' -o name) -- psql |
| 133 | +``` |
| 134 | + |
| 135 | +Create a table called `abc` that is identical to the table in the `rhino` database: |
| 136 | + |
| 137 | +``` |
| 138 | +CREATE TABLE abc (id int PRIMARY KEY); |
| 139 | +``` |
| 140 | + |
| 141 | +Finally, create a [subscription](https://www.postgresql.org/docs/current/logical-replication-subscription.html) that will manage the data replication from `rhino` into `hippo`: |
| 142 | + |
| 143 | +``` |
| 144 | +CREATE SUBSCRIPTION zoo |
| 145 | + CONNECTION 'host=rhino-primary.postgres-operator.svc user=logic dbname=zoo password=<LOGIC-PASSWORD>' |
| 146 | + PUBLICATION zoo; |
| 147 | +``` |
| 148 | + |
| 149 | +In a few moments, you should see the data replicated into your table: |
| 150 | + |
| 151 | +``` |
| 152 | +TABLE abc; |
| 153 | +``` |
| 154 | + |
| 155 | +which yields: |
| 156 | + |
| 157 | +``` |
| 158 | + id |
| 159 | +---- |
| 160 | + 1 |
| 161 | + 2 |
| 162 | + 3 |
| 163 | + 4 |
| 164 | + 5 |
| 165 | + 6 |
| 166 | + 7 |
| 167 | + 8 |
| 168 | + 9 |
| 169 | + 10 |
| 170 | +(10 rows) |
| 171 | +``` |
| 172 | + |
| 173 | +You can further test that logical replication is working by modifying the data on `rhino` in the `abc` table, and the verifying that it is replicated into `hippo`. |
0 commit comments