Skip to content

Instantly share code, notes, and snippets.

@phan2410
Forked from aleksasiriski/patronitocnpg.md
Created July 16, 2025 09:52
Show Gist options
  • Save phan2410/271ef0fa8ccdedd330693d192b6df907 to your computer and use it in GitHub Desktop.
Save phan2410/271ef0fa8ccdedd330693d192b6df907 to your computer and use it in GitHub Desktop.

Revisions

  1. @aleksasiriski aleksasiriski revised this gist Feb 25, 2024. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -43,4 +43,9 @@ postgresql:
    3. Apply the YAML and wait for all nodes to restart
    4. Connect to the database using generated SuperUser secret (use kubectl proxy to forward the connection)
    5. Run `ALTER DATABASE template1 REFRESH COLLATION VERSION;`
    6. Optionally disable SuperUser if there's no need for it
    6. Run these two commands on ALL of the databases in the cluster:
    ```
    REINDEX DATABASE <db_name>;
    ALTER DATABASE <db_name> REFRESH COLLATION VERSION;
    ```
    7. Optionally disable SuperUser if there's no need for it
  2. @aleksasiriski aleksasiriski revised this gist Feb 24, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,8 @@ postgresql:
    3. Find out on which node the first pod is running, ssh into it and `cd` into the dir which holds the pgdata volume (`/var/lib/rancher/k3s/storage/pvc-*/pgdata`)
    4. Inside pgdata volume edit `postgresql.conf`:

    - change pg_hba path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
    - change pg_ident path from `/var/lib/postgresql/15/main/pg_ident.conf` to `/var/lib/postgresql/data/pgdata/pg_ident.conf`
    - change `hba_file` path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
    - change `ident_file` path from `/var/lib/postgresql/15/main/pg_ident.conf` to `/var/lib/postgresql/data/pgdata/pg_ident.conf`
    - add `include 'custom.conf'` at the end of the file
    - add `include 'override.conf'` at the end of the file

  3. @aleksasiriski aleksasiriski revised this gist Feb 4, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ postgresql:
    1. Configure cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2. After starting the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    3. Find out on which node the first pod is running, ssh into it and `cd` into the dir which holds the pgdata volume
    3. Find out on which node the first pod is running, ssh into it and `cd` into the dir which holds the pgdata volume (`/var/lib/rancher/k3s/storage/pvc-*/pgdata`)
    4. Inside pgdata volume edit `postgresql.conf`:

    - change pg_hba path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
  4. @aleksasiriski aleksasiriski revised this gist Feb 4, 2024. 1 changed file with 20 additions and 17 deletions.
    37 changes: 20 additions & 17 deletions patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -7,12 +7,13 @@
    - Empty files called `custom.conf` and `override.conf` inside pgdata folder on all nodes (folder where `postgresql.conf` is)
    - User named strictly `streaming_replica` with REPLICATION ROLE attached (if it's not `streaming_replica` cnpg will fail because it needs that user to exist)
    - Patroni dynamic config:

    ```yaml
    postgresql:
    parameters:
    listen: '*'
    listen: "*"
    max_wal_senders: 5
    unix_socket_directories: '/controller/run'
    unix_socket_directories: "/controller/run"
    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    # ...other lines for patroni replication...
    @@ -23,21 +24,23 @@ postgresql:
    ## Creating cloudnative-pg cluster in replica mode
    1) Configure cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2) After starting the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    3) Find out on which node the first pod is running, ssh into it and `cd` into the dir which holds the pgdata volume
    4) Inside pgdata volume edit `postgresql.conf`:
    - change pg_hba path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
    - change pg_ident path from `/var/lib/postgresql/15/main/pg_ident.conf` to `/var/lib/postgresql/data/pgdata/pg_ident.conf`
    - add `include 'custom.conf'` at the end of the file
    - add `include 'override.conf'` at the end of the file
    5) Restart the pod and it should start up correctly, after it all of the other pods will replicate from the first one as well
    1. Configure cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2. After starting the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    3. Find out on which node the first pod is running, ssh into it and `cd` into the dir which holds the pgdata volume
    4. Inside pgdata volume edit `postgresql.conf`:

    - change pg_hba path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
    - change pg_ident path from `/var/lib/postgresql/15/main/pg_ident.conf` to `/var/lib/postgresql/data/pgdata/pg_ident.conf`
    - add `include 'custom.conf'` at the end of the file
    - add `include 'override.conf'` at the end of the file

    5. Restart the pod and it should start up correctly, after it all of the other pods will replicate from the first one as well

    ## Using the new cnpg-cluster

    1) Disable replica mode
    2) Enable SuperUser access
    3) Apply the YAML and wait for all nodes to restart
    4) Connect to the database using generated SuperUser secret (use kubectl proxy to forward the connection)
    5) Run `ALTER DATABASE template1 REFRESH COLLATION VERSION;`
    6) Optionally disable SuperUser if there's no need for it
    1. Disable replica mode
    2. Enable SuperUser access
    3. Apply the YAML and wait for all nodes to restart
    4. Connect to the database using generated SuperUser secret (use kubectl proxy to forward the connection)
    5. Run `ALTER DATABASE template1 REFRESH COLLATION VERSION;`
    6. Optionally disable SuperUser if there's no need for it
  5. @aleksasiriski aleksasiriski revised this gist Feb 4, 2024. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ postgresql:
    - host all all 0.0.0.0/0 md5
    ```
    ## Creating cloudnative-pg cluster
    ## Creating cloudnative-pg cluster in replica mode
    1) Configure cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2) After starting the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    @@ -31,4 +31,13 @@ postgresql:
    - change pg_ident path from `/var/lib/postgresql/15/main/pg_ident.conf` to `/var/lib/postgresql/data/pgdata/pg_ident.conf`
    - add `include 'custom.conf'` at the end of the file
    - add `include 'override.conf'` at the end of the file
    5) Restart the pod and it should start up correctly, after it all of the other pods will replicate from the first one as well
    5) Restart the pod and it should start up correctly, after it all of the other pods will replicate from the first one as well

    ## Using the new cnpg-cluster

    1) Disable replica mode
    2) Enable SuperUser access
    3) Apply the YAML and wait for all nodes to restart
    4) Connect to the database using generated SuperUser secret (use kubectl proxy to forward the connection)
    5) Run `ALTER DATABASE template1 REFRESH COLLATION VERSION;`
    6) Optionally disable SuperUser if there's no need for it
  6. @aleksasiriski aleksasiriski revised this gist Feb 2, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    ## Prerequisites

    - Stable connection to Patroni cluster from cnpg nodes
    - Same major PostgreSQL version (for example 15.1, cnpg image tag needs to correspond to that)
    - Empty files called `custom.conf` and `override.conf` inside pgdata folder on all nodes (folder where `postgresql.conf` is)
    - User named strictly `streaming_replica` with REPLICATION ROLE attached (if it's not `streaming_replica` cnpg will fail because it needs that user to exist)
    - Patroni dynamic config:
  7. @aleksasiriski aleksasiriski revised this gist Feb 2, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ postgresql:
    1) Configure cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2) After starting the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    3) Find on which node the first pod is starting, ssh into it and `cd` into the dir which holds the pgdata volume
    3) Find out on which node the first pod is running, ssh into it and `cd` into the dir which holds the pgdata volume
    4) Inside pgdata volume edit `postgresql.conf`:
    - change pg_hba path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
    - change pg_ident path from `/var/lib/postgresql/15/main/pg_ident.conf` to `/var/lib/postgresql/data/pgdata/pg_ident.conf`
  8. @aleksasiriski aleksasiriski revised this gist Feb 2, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ postgresql:
    ## Creating cloudnative-pg cluster
    1) Configure cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2) After started the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    2) After starting the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    3) Find on which node the first pod is starting, ssh into it and `cd` into the dir which holds the pgdata volume
    4) Inside pgdata volume edit `postgresql.conf`:
    - change pg_hba path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
  9. @aleksasiriski aleksasiriski revised this gist Feb 2, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ postgresql:
    ## Creating cloudnative-pg cluster
    1) Configupre cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    1) Configure cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2) After started the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    3) Find on which node the first pod is starting, ssh into it and `cd` into the dir which holds the pgdata volume
    4) Inside pgdata volume edit `postgresql.conf`:
  10. @aleksasiriski aleksasiriski created this gist Feb 2, 2024.
    33 changes: 33 additions & 0 deletions patronitocnpg.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # Migrate Patroni PostgreSQL cluster to cloudnative-pg

    ## Prerequisites

    - Stable connection to Patroni cluster from cnpg nodes
    - Empty files called `custom.conf` and `override.conf` inside pgdata folder on all nodes (folder where `postgresql.conf` is)
    - User named strictly `streaming_replica` with REPLICATION ROLE attached (if it's not `streaming_replica` cnpg will fail because it needs that user to exist)
    - Patroni dynamic config:
    ```yaml
    postgresql:
    parameters:
    listen: '*'
    max_wal_senders: 5
    unix_socket_directories: '/controller/run'
    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    # ...other lines for patroni replication...
    - host replication streaming_replica 0.0.0.0/0 md5
    - host all all 127.0.0.1/32 md5
    - host all all 0.0.0.0/0 md5
    ```
    ## Creating cloudnative-pg cluster
    1) Configupre cnpg cluster to connect to Patroni via pgbasebackup and use that source-db as bootstrap and replica mode
    2) After started the cluster in replica mode, first init pod should succeed but afterwards the first cnpg pod will fail to start
    3) Find on which node the first pod is starting, ssh into it and `cd` into the dir which holds the pgdata volume
    4) Inside pgdata volume edit `postgresql.conf`:
    - change pg_hba path from `/var/lib/postgresql/15/main/pg_hba.conf` to `/var/lib/postgresql/data/pgdata/pg_hba.conf`
    - change pg_ident path from `/var/lib/postgresql/15/main/pg_ident.conf` to `/var/lib/postgresql/data/pgdata/pg_ident.conf`
    - add `include 'custom.conf'` at the end of the file
    - add `include 'override.conf'` at the end of the file
    5) Restart the pod and it should start up correctly, after it all of the other pods will replicate from the first one as well