Skip to content

Commit 4f8ed33

Browse files
authored
Add PostgreSQL 14 to 16 data migration guide (#1153)
* refactor development section * add content to migration guide * fixes * remove launchpad mailing list * Update deployment channel to 16/edge for consistency
1 parent 0adfc1f commit 4f8ed33

18 files changed

+169
-50
lines changed

docs/explanation/interfaces-and-endpoints.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Adding a relation is accomplished with `juju relate` (or `juju integrate` for Ju
1616

1717
```text
1818
# Deploy Charmed PostgreSQL cluster with 3 nodes
19-
juju deploy postgresql-k8s --channel 16/stable -n 3 --trust
19+
juju deploy postgresql-k8s --channel 16/edge -n 3 --trust
2020
2121
# Deploy the relevant application charms
2222
juju deploy mycharm
@@ -47,7 +47,7 @@ Check the limitations of legacy interface implementations in [](/explanation/leg
4747
This charm supports legacy interface `pgsql` from the previous [PostgreSQL charm](https://launchpad.net/postgresql-charm):
4848

4949
```text
50-
juju deploy postgresql-k8s --channel 16/stable --trust
50+
juju deploy postgresql-k8s --channel 16/edge --trust
5151
juju deploy finos-waltz-k8s --channel edge
5252
juju relate postgresql-k8s:db finos-waltz-k8s
5353
```

docs/explanation/legacy-charm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Please find the list of supported PostgreSQL [Extensions](/reference/plugins-ext
7272

7373
## Roles supported by modern charm
7474

75-
In the legacy charm, the user could request roles by setting the `roles` field to a comma separated list of desired roles. It is NOT supported by the modern charm implementation of the legacy `pgsql` interface. The same functionality is provided via the modern `postgresql_client` using "[extra-user-roles](/explanation/users)". Please check how to [migrate on the new interface](/how-to/development/integrate-with-your-charm).
75+
In the legacy charm, the user could request roles by setting the `roles` field to a comma separated list of desired roles. It is NOT supported by the modern charm implementation of the legacy `pgsql` interface. The same functionality is provided via the modern `postgresql_client` using "[extra-user-roles](/explanation/users)". Please check how to [migrate on the new interface](/how-to/integrate-with-your-charm).
7676

7777
## Supported PostgreSQL versions by modern charm
7878

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Migrate data
2+
3+
For guidance about moving data from a Charmed PostgreSQL 14 database to Charmed PostgreSQL 16, start here:
4+
5+
```{toctree}
6+
:titlesonly:
7+
8+
Migrate data from PostgreSQL 14 to 16 <migrate-data-from-14-to-16>
9+
```
10+
11+
More generic data migration guides:
12+
13+
```{toctree}
14+
:titlesonly:
15+
16+
Migrate data via `pg_dump` <migrate-data-via-pg-dump>
17+
Migrate data via backup/restore <migrate-data-via-backup-restore>
18+
```
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Migrate data from PostgreSQL 14 to 16
2+
3+
There are two possible ways to migrate data from PostgreSQL 14 to 16 depending on how roles are managed:
4+
5+
**In case of admin roles management**, all database objects ownership are handled manually by the user. In this case, see the more general guide {ref}`migrate-data-via-pg-dump`. Note that you must set `extra-user-roles` to `charmed-admin` once a Juju relation is requested the new database.
6+
7+
**In the case of charm roles management**, all the database objects ownership will be handled by charm automatically. This guide covers how to migrate data from Charmed PostgreSQL 14 to 16 using the new charm roles management setup for client applications managed by Juju. The migrated data from PostgreSQL 14 will be mapped to the corresponding ownership in PostgreSQL 16.
8+
9+
## Prepare PostgreSQL 14 data
10+
11+
First, in order to make sure the latest data is included, remove the relation between the client app and Charmed PostgreSQL 14.
12+
13+
Then, define the following variables for the old database:
14+
15+
```shell
16+
TMP_PATH="~/old-db-dump/"
17+
OLD_DB_NAME="postgresql_test_app_database"
18+
OLD_IP="10.218.34.229"
19+
OLD_USER="operator"
20+
OLD_PASSWORD="fJQYljbthEo2T1gj"
21+
```
22+
23+
Create a dump of the old PostgreSQL 14 database with `pg_dump`:
24+
25+
```shell
26+
mkdir -p "${TMP_PATH}"
27+
PGPASSWORD="${OLD_PASSWORD}" pg_dump -j 4 -Fd -h "${OLD_IP}" -U "${OLD_USER}" -d "${OLD_DB_NAME}" -f "${TMP_PATH}" --compress 9
28+
```
29+
30+
## Set up new PostgreSQL 16 charm
31+
32+
Deploy one unit of Charmed PostgreSQL 16. This will simplify the migration and can be scaled later.
33+
34+
```shell
35+
juju deploy postgresql-k8s --channel 16/edge --trust
36+
```
37+
38+
Define the following variables for the new database:
39+
40+
```shell
41+
NEW_DB_NAME="postgresql_test_app_database123"
42+
NEW_IP="10.218.34.56"
43+
NEW_USER="operator"
44+
NEW_PASSWORD="RnnijCiotVeW8O5I"
45+
NEW_OWNER="charmed_${NEW_DB_NAME}_owner"
46+
```
47+
48+
Migrate the following charm features from the old 14 charm to the new 16 charm:
49+
* any necessary charm config options
50+
* enabled charm extensions/plugins
51+
52+
```{note}
53+
Config options and extensions *must* be migrated before restoring the data dump
54+
```
55+
56+
## Create a new database on PostgreSQL 16
57+
58+
```shell
59+
PGPASSWORD="${NEW_PASSWORD}" psql -h "${NEW_IP}" -U "${NEW_USER}" -d postgres -c "CREATE DATABASE ${NEW_DB_NAME}"
60+
```
61+
62+
Create new roles by running the function `set_up_predefined_catalog_roles()` in all databases except `postgres` and `template1`. It will create roles like `charmed_<database-name>_owner`, `..._dml` and others:
63+
64+
```shell
65+
PGPASSWORD="${NEW_PASSWORD}" psql -h "${NEW_IP}" -U "${NEW_USER}" -d "${NEW_DB_NAME}" -c "SELECT set_up_predefined_catalog_roles();"
66+
PGPASSWORD="${NEW_PASSWORD}" psql -h "${NEW_IP}" -U "${NEW_USER}" -d "${NEW_DB_NAME}" -c "ALTER DATABASE ${NEW_DB_NAME} OWNER TO charmed_databases_owner;"
67+
PGPASSWORD="${NEW_PASSWORD}" psql -h "${NEW_IP}" -U "${NEW_USER}" -d "${NEW_DB_NAME}" -c "ALTER SCHEMA public OWNER TO ${NEW_OWNER};"
68+
```
69+
70+
## Migrate data from PostgreSQL 14 to 16
71+
72+
Restore the PostgreSQL 14 database dump into the new 16 database:
73+
74+
```shell
75+
PGPASSWORD="${NEW_PASSWORD}" pg_restore -j 4 -h "${NEW_IP}" -U "${NEW_USER}" -d "${NEW_DB_NAME}" -Fd "${TMP_PATH}" --no-owner
76+
```
77+
78+
### Set up new database ownership
79+
80+
Verify and modify the ownership for each database object in each schema to be equal to `charmed_<database-name>_owner` (`${NEW_OWNER}` above).
81+
82+
For example, to find and fix ownership for all tables, sequences, and views:
83+
84+
```shell
85+
PGPASSWORD="${NEW_PASSWORD}" psql -h "${NEW_IP}" -U "${NEW_USER}" -d "${NEW_DB_NAME}"
86+
87+
mydb=> DO $$
88+
DECLARE
89+
r record;
90+
BEGIN
91+
FOR r IN
92+
SELECT format('ALTER %s %I.%I OWNER TO %I;',
93+
CASE c.relkind
94+
WHEN 'r' THEN 'TABLE'
95+
WHEN 'v' THEN 'VIEW'
96+
WHEN 'm' THEN 'MATERIALIZED VIEW'
97+
WHEN 'S' THEN 'SEQUENCE'
98+
WHEN 'p' THEN 'TABLE'
99+
ELSE NULL END,
100+
n.nspname, c.relname, 'charmed_<database-name>_owner')
101+
FROM pg_class c
102+
JOIN pg_namespace n ON n.oid = c.relnamespace
103+
WHERE n.nspname = 'public'
104+
LOOP
105+
EXECUTE r.format;
106+
END LOOP;
107+
END$$;
108+
```
109+
110+
At this stage, the database has been completely imported. The cluster can be scaled, and the client app can be related to the new PostgreSQL 16 database.

docs/how-to/development/migrate-data-via-backup-restore.md renamed to docs/how-to/data-migration/migrate-data-via-backup-restore.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Migrate database data using ‘backup/restore’
1+
(migrate-data-via-backup-restore)=
2+
# Migrate data via backup/restore
23

3-
This is a guide for migrating data from modern charms. To migrate [legacy charms](/explanation/legacy-charm) data, refer to the guide [Migrate data via pg_dump](/how-to/development/migrate-data-via-pg-dump).
4+
This is a guide for migrating data from modern charms. To migrate [legacy charms](/explanation/legacy-charm) data, refer to the guide [Migrate data via pg_dump](/how-to/data-migration/migrate-data-via-pg-dump).
45

56
This Charmed PostgreSQL K8s operator is able to restore its own [backups](/how-to/back-up-and-restore/restore-a-backup) stored on [S3-compatible storage](/how-to/back-up-and-restore/configure-s3-aws). The same restore approach is applicable to [restore backups made by a different installation](/how-to/back-up-and-restore/migrate-a-cluster) of Charmed PostgreSQL, or even another PostgreSQL charm. The backups have to be created manually using [pgBackRest](https://pgbackrest.org/)!
67

@@ -21,7 +22,7 @@ Below is the *general approach* to the migration (see warning above!):
2122

2223
1. **Retrieve root/admin-level credentials from the legacy charm.**
2324

24-
See examples [here](/how-to/development/migrate-data-via-pg-dump).
25+
See examples [here](/how-to/data-migration/migrate-data-via-pg-dump).
2526

2627
2. **Install [pgBackRest](https://pgbackrest.org/) inside the old charm OR nearby.**
2728

docs/how-to/development/migrate-data-via-pg-dump.md renamed to docs/how-to/data-migration/migrate-data-via-pg-dump.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
# Migrate database data using `pg_dump` / `pg_restore`
1+
(migrate-data-via-pg-dump)=
2+
# Migrate database data using `pg_dump`
23

3-
This document describes database **data** migration only. To migrate charms on new juju interfaces, refer to the guide [How to integrate a database with my charm](/how-to/development/integrate-with-your-charm).
4+
This document describes database **data** migration only. To migrate charms on new juju interfaces, refer to the guide [How to integrate a database with my charm](/how-to/integrate-with-your-charm).
45

5-
## Do you need to migrate?
6-
7-
A database migration is only required if the output of the following command is `latest/stable`:
8-
9-
```text
10-
juju show-application postgresql-k8s | yq '.[] | .channel'
11-
```
12-
Migration is **not** necessary if the output above is `14/stable` or `16/edge`.
13-
14-
This guide can be used to copy data between different installations of the same (modern) charm `postgresql-k8s`, but the [backup/restore](/how-to/development/migrate-data-via-backup-restore) is more recommended for migrations between modern charms.
15-
16-
## Summary
17-
18-
The legacy K8s charm are archived in the `latest/stable` channel (read more [here](/explanation/legacy-charm)).
196
A minor difference in commands might be necessary for different revisions and/or Juju versions, but the general logic remains:
207

218
* Deploy the modern charm nearby
@@ -61,7 +48,7 @@ OLD_DB_USER=$(juju show-unit ${CLIENT_APP} | yq '.[] | .relation-info | select(.
6148
Deploy new PostgreSQL database charm:
6249

6350
```text
64-
juju deploy postgresql-k8s ${NEW_DB_APP} --channel 16/stable --trust
51+
juju deploy postgresql-k8s ${NEW_DB_APP} --channel 16/edge --trust
6552
```
6653

6754
Obtain `operator` user password of new PostgreSQL database from PostgreSQL charm:

docs/how-to/deploy/aks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ juju model-config logging-config='<root>=INFO;unit=DEBUG'
130130
The following command deploys PostgreSQL K8s:
131131

132132
```text
133-
juju deploy postgresql-k8s --channel 16/stable --trust -n 3
133+
juju deploy postgresql-k8s --channel 16/edge --trust -n 3
134134
```
135135
Sample output:
136136
```text

docs/how-to/deploy/canonical-k8s.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ juju bootstrap ck8s
4949

5050
```text
5151
juju add-model postgresql
52-
juju deploy postgresql-k8s --channel 16/stable --trust
52+
juju deploy postgresql-k8s --channel 16/edge --trust
5353
```
5454

5555
follow the deployment progress using:

docs/how-to/deploy/gke.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ kubectl get pods -n welcome-model
9191
The following commands deploy PostgreSQL K8s and PgBouncer K8s:
9292

9393
```text
94-
juju deploy postgresql-k8s --channel 16/stable --trust
94+
juju deploy postgresql-k8s --channel 16/edge --trust
9595
juju deploy pgbouncer-k8s --trust
9696
```
9797

docs/how-to/deploy/multi-az.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ The command below demonstrates how to deploy Charmed PostgreSQL K8s with Juju co
7777

7878
```text
7979
export MYAPP="mydatabase" ; \
80-
juju deploy postgresql-k8s --channel 16/stable ${MYAPP} --trust -n 3 \
80+
juju deploy postgresql-k8s --channel 16/edge ${MYAPP} --trust -n 3 \
8181
--constraints="tags=anti-pod.app.kubernetes.io/name=${MYAPP},anti-pod.topology-key=topology.kubernetes.io/zone"
8282
```
8383

0 commit comments

Comments
 (0)