-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.html
More file actions
1098 lines (1064 loc) · 52.6 KB
/
index.html
File metadata and controls
1098 lines (1064 loc) · 52.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="author" content="The CloudNativePG Contributors" />
<link rel="shortcut icon" href="../img/favicon.ico" />
<title>Bootstrap - CloudNativePG v1.25</title>
<link rel="stylesheet" href="../css/theme.css" />
<link rel="stylesheet" href="../css/theme_extra.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" />
<link href="../css/override.css" rel="stylesheet" />
<script>
// Current page data
var mkdocs_page_name = "Bootstrap";
var mkdocs_page_input_path = "bootstrap.md";
var mkdocs_page_url = null;
</script>
<!--[if lt IE 9]>
<script src="../js/html5shiv.min.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href=".." class="icon icon-home"> CloudNativePG v1.25
</a><div role="search">
<form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" title="Type search term here" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="..">CloudNativePG</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../before_you_start/">Before You Start</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../use_cases/">Use cases</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../architecture/">Architecture</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation_upgrade/">Installation and upgrades</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../quickstart/">Quickstart</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../image_catalog/">Image Catalog</a>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal current" href="#">Bootstrap</a>
<ul class="current">
<li class="toctree-l2"><a class="reference internal" href="#the-bootstrap-section">The bootstrap section</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#the-externalclusters-section">The externalClusters section</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#password-files">Password files</a>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#bootstrap-an-empty-cluster-initdb">Bootstrap an empty cluster (initdb)</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#passing-options-to-initdb">Passing Options to initdb</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#executing-queries-after-initialization">Executing Queries After Initialization</a>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#bootstrap-from-another-cluster">Bootstrap from another cluster</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#bootstrap-from-a-backup-recovery">Bootstrap from a backup (recovery)</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#bootstrap-from-a-live-cluster-pg_basebackup">Bootstrap from a live cluster (pg_basebackup)</a>
<ul>
<li class="toctree-l4"><a class="reference internal" href="#requirements">Requirements</a>
</li>
<li class="toctree-l4"><a class="reference internal" href="#about-the-replication-user">About the replication user</a>
</li>
<li class="toctree-l4"><a class="reference internal" href="#usernamepassword-authentication">Username/Password authentication</a>
</li>
<li class="toctree-l4"><a class="reference internal" href="#tls-certificate-authentication">TLS certificate authentication</a>
</li>
<li class="toctree-l4"><a class="reference internal" href="#configure-the-application-database">Configure the application database</a>
</li>
<li class="toctree-l4"><a class="reference internal" href="#current-limitations">Current limitations</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database_import/">Importing Postgres databases</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../security/">Security</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../instance_manager/">Postgres instance manager</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../scheduling/">Scheduling</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../resource_management/">Resource management</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../failure_modes/">Failure Modes</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../rolling_update/">Rolling Updates</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../replication/">Replication</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../logical_replication/">Logical Replication</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../backup/">Backup</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../backup_barmanobjectstore/">Backup on object stores</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../wal_archiving/">WAL archiving</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../backup_volumesnapshot/">Backup on volume snapshots</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../recovery/">Recovery</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../service_management/">Service Management</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../postgresql_conf/">PostgreSQL Configuration</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../declarative_role_management/">PostgreSQL Role Management</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../declarative_database_management/">PostgreSQL Database Management</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tablespaces/">Tablespaces</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../operator_conf/">Operator configuration</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cluster_conf/">Instance pod configuration</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../storage/">Storage</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../labels_annotations/">Labels and annotations</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../monitoring/">Monitoring</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../logging/">Logging</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../certificates/">Certificates</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../ssl_connections/">Client TLS/SSL connections</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../applications/">Connecting from an application</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../connection_pooling/">Connection pooling</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../replica_cluster/">Replica clusters</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../kubernetes_upgrade/">Kubernetes Upgrade and Maintenance</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../kubectl-plugin/">Kubectl Plugin</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../failover/">Automated failover</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../troubleshooting/">Troubleshooting</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../fencing/">Fencing</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../declarative_hibernation/">Declarative hibernation</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../postgis/">PostGIS</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../e2e/">End-to-End Tests</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../container_images/">Container Image Requirements</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../operator_capability_levels/">Operator capability levels</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../controller/">Custom Pod Controller</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../samples/">Examples</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../networking/">Networking</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../benchmarking/">Benchmarking</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq/">Frequently Asked Questions (FAQ)</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cloudnative-pg.v1/">API Reference</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../supported_releases/">Supported releases</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../preview_version/">Preview Versions</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../release_notes/">Release notes</a>
</li>
</ul>
<p class="caption"><span class="caption-text">CNCF Projects Integrations</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cncf-projects/external-secrets/">External Secrets</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../cncf-projects/cilium/">Cilium</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Appendixes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../appendixes/object_stores/">Appendix A - Common object stores for backups</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="..">CloudNativePG v1.25</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href=".." class="icon icon-home" aria-label="Docs"></a></li>
<li class="breadcrumb-item active">Bootstrap</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div class="section" itemprop="articleBody">
<h1 id="bootstrap">Bootstrap</h1>
<!-- SPDX-License-Identifier: CC-BY-4.0 -->
<p>This section describes the options available to create a new
PostgreSQL cluster and the design rationale behind them.
There are primarily two ways to bootstrap a new cluster:</p>
<ul>
<li>from scratch (<code>initdb</code>)</li>
<li>from an existing PostgreSQL cluster, either directly (<code>pg_basebackup</code>)
or indirectly through a physical base backup (<code>recovery</code>)</li>
</ul>
<p>The <code>initdb</code> bootstrap also provides the option to import one or more
databases from an existing PostgreSQL cluster, even if it's outside
Kubernetes or running a different major version of PostgreSQL.
For more detailed information about this feature, please refer to the
<a href="../database_import/">"Importing Postgres databases"</a> section.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Bootstrapping from an existing cluster enables the creation of a
<strong>replica cluster</strong>—an independent PostgreSQL cluster that remains in
continuous recovery, stays synchronized with the source cluster, and
accepts read-only connections.
For more details, refer to the <a href="../replica_cluster/">Replica Cluster section</a>.</p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>CloudNativePG requires both the <code>postgres</code> user and database to
always exist. Using the local Unix Domain Socket, it needs to connect
as the <code>postgres</code> user to the <code>postgres</code> database via <code>peer</code> authentication in
order to perform administrative tasks on the cluster.
<strong>DO NOT DELETE</strong> the <code>postgres</code> user or the <code>postgres</code> database!!!</p>
</div>
<div class="admonition info">
<p class="admonition-title">Info</p>
<p>CloudNativePG is gradually introducing support for
<a href="https://github.com/cloudnative-pg/cloudnative-pg/issues/2081">Kubernetes' native <code>VolumeSnapshot</code> API</a>
for both incremental and differential copy in backup and recovery
operations - if supported by the underlying storage classes.
Please see <a href="../recovery/#recovery-from-volumesnapshot-objects">"Recovery from Volume Snapshot objects"</a>
for details.</p>
</div>
<h2 id="the-bootstrap-section">The <code>bootstrap</code> section</h2>
<p>The <em>bootstrap</em> method can be defined in the <code>bootstrap</code> section of the cluster
specification. CloudNativePG currently supports the following bootstrap methods:</p>
<ul>
<li><code>initdb</code>: initialize a new PostgreSQL cluster (default)</li>
<li><code>recovery</code>: create a PostgreSQL cluster by restoring from a base backup of an
existing cluster and, if needed, replaying all the available WAL files or up to
a given <em>point in time</em></li>
<li><code>pg_basebackup</code>: create a PostgreSQL cluster by cloning an existing one of
the same major version using <code>pg_basebackup</code> through the streaming
replication protocol. This method is particularly useful for migrating
databases to CloudNativePG, although meeting all requirements can be
challenging. Be sure to review the warnings in the
<a href="#bootstrap-from-a-live-cluster-pg_basebackup"><code>pg_basebackup</code> subsection</a>
carefully.</li>
</ul>
<p>Only one bootstrap method can be specified in the manifest.
Attempting to define multiple bootstrap methods will result in validation errors.</p>
<p>In contrast to the <code>initdb</code> method, both <code>recovery</code> and <code>pg_basebackup</code>
create a new cluster based on another one (either offline or online) and can be
used to spin up replica clusters. They both rely on the definition of external
clusters.
Refer to the <a href="../replica_cluster/">replica cluster section</a> for more information.</p>
<p>Given the amount of possible backup methods and combinations of backup
storage that the CloudNativePG operator provides for <code>recovery</code>, please refer to
the dedicated <a href="../recovery/">"Recovery" section</a> for guidance on each method.</p>
<div class="admonition seealso">
<p class="admonition-title">API reference</p>
<p>Please refer to the <a href="../cloudnative-pg.v1/#postgresql-cnpg-io-v1-BootstrapConfiguration">"API reference for the <code>bootstrap</code> section</a>
for more information.</p>
</div>
<h2 id="the-externalclusters-section">The <code>externalClusters</code> section</h2>
<p>The <code>externalClusters</code> section of the cluster manifest can be used to configure
access to one or more PostgreSQL clusters as <em>sources</em>.
The primary use cases include:</p>
<ol>
<li><strong>Importing Databases:</strong> Specify an external source to be utilized during
the <a href="../database_import/">importation of databases</a> via logical backup and
restore, as part of the <code>initdb</code> bootstrap method.</li>
<li><strong>Cross-Region Replication:</strong> Define a cross-region PostgreSQL cluster
employing physical replication, capable of extending across distinct Kubernetes
clusters or traditional VM/bare-metal environments.</li>
<li><strong>Recovery from Physical Base Backup:</strong> Recover, fully or at a
given Point-In-Time, a PostgreSQL cluster by referencing a physical base
backup.</li>
</ol>
<div class="admonition info">
<p class="admonition-title">Info</p>
<p>Ongoing development will extend the functionality of <code>externalClusters</code> to
accommodate additional use cases, such as logical replication and foreign
servers in future releases.</p>
</div>
<p>As far as bootstrapping is concerned, <code>externalClusters</code> can be used
to define the source PostgreSQL cluster for either the <code>pg_basebackup</code>
method or the <code>recovery</code> one. An external cluster needs to have:</p>
<ul>
<li>a name that identifies the external cluster, to be used as a reference via the
<code>source</code> option</li>
<li>
<p>at least one of the following:</p>
<ul>
<li>information about streaming connection</li>
<li>information about the <strong>recovery object store</strong>, which is a Barman Cloud
compatible object store that contains:<ul>
<li>the WAL archive (required for Point In Time Recovery)</li>
<li>the catalog of physical base backups for the Postgres cluster</li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>A recovery object store is normally an AWS S3, Azure Blob Storage,
or Google Cloud Storage source that is managed by Barman Cloud.</p>
</div>
<p>When only the streaming connection is defined, the source can be used for the
<code>pg_basebackup</code> method. When only the recovery object store is defined, the
source can be used for the <code>recovery</code> method. When both are defined, any of
the two bootstrap methods can be chosen. The following table summarizes your
options:</p>
<table>
<thead>
<tr>
<th style="text-align: left;">Content of externalClusters</th>
<th style="text-align: center;">pg_basebackup</th>
<th style="text-align: center;">recovery</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">Only streaming</td>
<td style="text-align: center;">✓</td>
<td style="text-align: center;"></td>
</tr>
<tr>
<td style="text-align: left;">Only object store</td>
<td style="text-align: center;"></td>
<td style="text-align: center;">✓</td>
</tr>
<tr>
<td style="text-align: left;">Streaming and object store</td>
<td style="text-align: center;">✓</td>
<td style="text-align: center;">✓</td>
</tr>
</tbody>
</table>
<p>Furthermore, in case of <code>pg_basebackup</code> or full <code>recovery</code> point in time, the
cluster is eligible for replica cluster mode. This means that the cluster is
continuously fed from the source, either via streaming, via WAL shipping
through the PostgreSQL's <code>restore_command</code>, or any of the two.</p>
<div class="admonition seealso">
<p class="admonition-title">API reference</p>
<p>Please refer to the <a href="../cloudnative-pg.v1/#postgresql-cnpg-io-v1-ExternalCluster">"API reference for the <code>externalClusters</code> section</a>
for more information.</p>
</div>
<h3 id="password-files">Password files</h3>
<p>Whenever a password is supplied within an <code>externalClusters</code> entry,
CloudNativePG autonomously manages a <a href="https://www.postgresql.org/docs/current/libpq-pgpass.html">PostgreSQL password file</a>
for it, residing at <code>/controller/external/NAME/pgpass</code> in each instance.</p>
<p>This approach enables CloudNativePG to securely establish connections with an
external server without exposing any passwords in the connection string.
Instead, the connection safely references the aforementioned file through the
<code>passfile</code> connection parameter.</p>
<h2 id="bootstrap-an-empty-cluster-initdb">Bootstrap an empty cluster (<code>initdb</code>)</h2>
<p>The <code>initdb</code> bootstrap method is used to create a new PostgreSQL cluster from
scratch. It is the default one unless specified differently.</p>
<p>The following example contains the full structure of the <code>initdb</code>
configuration:</p>
<pre><code class="language-yaml">apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cluster-example-initdb
spec:
instances: 3
bootstrap:
initdb:
database: app
owner: app
secret:
name: app-secret
storage:
size: 1Gi
</code></pre>
<p>The above example of bootstrap will:</p>
<ol>
<li>create a new <code>PGDATA</code> folder using PostgreSQL's native <code>initdb</code> command</li>
<li>create an <em>unprivileged</em> user named <code>app</code></li>
<li>set the password of the latter (<code>app</code>) using the one in the <code>app-secret</code>
secret (make sure that <code>username</code> matches the same name of the <code>owner</code>)</li>
<li>create a database called <code>app</code> owned by the <code>app</code> user.</li>
</ol>
<p>Thanks to the <em>convention over configuration paradigm</em>, you can let the
operator choose a default database name (<code>app</code>) and a default application
user name (same as the database name), as well as randomly generate a
secure password for both the superuser and the application user in
PostgreSQL.</p>
<p>Alternatively, you can generate your password, store it as a secret,
and use it in the PostgreSQL cluster - as described in the above example.</p>
<p>The supplied secret must comply with the specifications of the
<a href="https://kubernetes.io/docs/concepts/configuration/secret/#basic-authentication-secret"><code>kubernetes.io/basic-auth</code> type</a>.
As a result, the <code>username</code> in the secret must match the one of the <code>owner</code>
(for the application secret) and <code>postgres</code> for the superuser one.</p>
<p>The following is an example of a <code>basic-auth</code> secret:</p>
<pre><code class="language-yaml">apiVersion: v1
data:
username: YXBw
password: cGFzc3dvcmQ=
kind: Secret
metadata:
name: app-secret
type: kubernetes.io/basic-auth
</code></pre>
<p>The application database is the one that should be used to store application
data. Applications should connect to the cluster with the user that owns
the application database.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>If you need to create additional users, please refer to
<a href="../declarative_role_management/">"Declarative database role management"</a>.</p>
</div>
<p>In case you don't supply any database name, the operator will proceed
by convention and create the <code>app</code> database, and adds it to the cluster
definition using a <em>defaulting webhook</em>.
The user that owns the database defaults to the database name instead.</p>
<p>The application user is not used internally by the operator, which instead
relies on the superuser to reconcile the cluster with the desired status.</p>
<h3 id="passing-options-to-initdb">Passing Options to <code>initdb</code></h3>
<p>The PostgreSQL data directory is initialized using the
<a href="https://www.postgresql.org/docs/current/app-initdb.html"><code>initdb</code> PostgreSQL command</a>.</p>
<p>CloudNativePG enables you to customize the behavior of <code>initdb</code> to modify
settings such as default locale configurations and data checksums.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>CloudNativePG acts only as a direct proxy to <code>initdb</code> for locale-related
options, due to the ongoing and significant enhancements in PostgreSQL's locale
support. It is your responsibility to ensure that the correct options are
provided, following the PostgreSQL documentation, and to verify that the
bootstrap process completes successfully.</p>
</div>
<p>To include custom options in the <code>initdb</code> command, you can use the following
parameters:</p>
<dl>
<dt>builtinLocale</dt>
<dd>When <code>builtinLocale</code> is set to a value, CloudNativePG passes it to the
<code>--builtin-locale</code> option in <code>initdb</code>. This option controls the builtin locale, as
defined in <a href="https://www.postgresql.org/docs/current/locale.html">"Locale Support"</a>
from the PostgreSQL documentation (default: empty). Note that this option requires
<code>localeProvider</code> to be set to <code>builtin</code>. Available from PostgreSQL 17.</dd>
<dt>dataChecksums</dt>
<dd>When <code>dataChecksums</code> is set to <code>true</code>, CloudNativePG invokes the <code>-k</code> option in
<code>initdb</code> to enable checksums on data pages and help detect corruption by the
I/O system - that would otherwise be silent (default: <code>false</code>).</dd>
<dt>encoding</dt>
<dd>When <code>encoding</code> set to a value, CloudNativePG passes it to the <code>--encoding</code>
option in <code>initdb</code>, which selects the encoding of the template database
(default: <code>UTF8</code>).</dd>
<dt>icuLocale</dt>
<dd>When <code>icuLocale</code> is set to a value, CloudNativePG passes it to the
<code>--icu-locale</code> option in <code>initdb</code>. This option controls the ICU locale, as
defined in <a href="https://www.postgresql.org/docs/current/locale.html">"Locale Support"</a>
from the PostgreSQL documentation (default: empty).
Note that this option requires <code>localeProvider</code> to be set to <code>icu</code>.
Available from PostgreSQL 15.</dd>
<dt>icuRules</dt>
<dd>When <code>icuRules</code> is set to a value, CloudNativePG passes it to the
<code>--icu-rules</code> option in <code>initdb</code>. This option controls the ICU locale, as
defined in <a href="https://www.postgresql.org/docs/current/locale.html">"Locale
Support"</a> from the
PostgreSQL documentation (default: empty). Note that this option requires
<code>localeProvider</code> to be set to <code>icu</code>. Available from PostgreSQL 16.</dd>
<dt>locale</dt>
<dd>When <code>locale</code> is set to a value, CloudNativePG passes it to the <code>--locale</code>
option in <code>initdb</code>. This option controls the locale, as defined in
<a href="https://www.postgresql.org/docs/current/locale.html">"Locale Support"</a> from
the PostgreSQL documentation. By default, the locale parameter is empty. In
this case, environment variables such as <code>LANG</code> are used to determine the
locale. Be aware that these variables can vary between container images,
potentially leading to inconsistent behavior.</dd>
<dt>localeCollate</dt>
<dd>When <code>localeCollate</code> is set to a value, CloudNativePG passes it to the <code>--lc-collate</code>
option in <code>initdb</code>. This option controls the collation order (<code>LC_COLLATE</code>
subcategory), as defined in <a href="https://www.postgresql.org/docs/current/locale.html">"Locale Support"</a>
from the PostgreSQL documentation (default: <code>C</code>).</dd>
<dt>localeCType</dt>
<dd>When <code>localeCType</code> is set to a value, CloudNativePG passes it to the <code>--lc-ctype</code> option in
<code>initdb</code>. This option controls the collation order (<code>LC_CTYPE</code> subcategory), as
defined in <a href="https://www.postgresql.org/docs/current/locale.html">"Locale Support"</a>
from the PostgreSQL documentation (default: <code>C</code>).</dd>
<dt>localeProvider</dt>
<dd>When <code>localeProvider</code> is set to a value, CloudNativePG passes it to the <code>--locale-provider</code>
option in <code>initdb</code>. This option controls the locale provider, as defined in
<a href="https://www.postgresql.org/docs/current/locale.html">"Locale Support"</a> from the
PostgreSQL documentation (default: empty, which means <code>libc</code> for PostgreSQL).
Available from PostgreSQL 15.</dd>
<dt>walSegmentSize</dt>
<dd>When <code>walSegmentSize</code> is set to a value, CloudNativePG passes it to the <code>--wal-segsize</code>
option in <code>initdb</code> (default: not set - defined by PostgreSQL as 16 megabytes).</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The only two locale options that CloudNativePG implements during
the <code>initdb</code> bootstrap refer to the <code>LC_COLLATE</code> and <code>LC_TYPE</code> subcategories.
The remaining locale subcategories can be configured directly in the PostgreSQL
configuration, using the <code>lc_messages</code>, <code>lc_monetary</code>, <code>lc_numeric</code>, and
<code>lc_time</code> parameters.</p>
</div>
<p>The following example enables data checksums and sets the default encoding to
<code>LATIN1</code>:</p>
<pre><code class="language-yaml">apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cluster-example-initdb
spec:
instances: 3
bootstrap:
initdb:
database: app
owner: app
dataChecksums: true
encoding: 'LATIN1'
storage:
size: 1Gi
</code></pre>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>CloudNativePG supports another way to customize the behavior of the
<code>initdb</code> invocation, using the <code>options</code> subsection. However, given that there
are options that can break the behavior of the operator (such as <code>--auth</code> or
<code>-d</code>), this technique is deprecated and will be removed from future versions of
the API.</p>
</div>
<h3 id="executing-queries-after-initialization">Executing Queries After Initialization</h3>
<p>You can specify a custom list of queries that will be executed once,
immediately after the cluster is created and configured. These queries will be
executed as the <em>superuser</em> (<code>postgres</code>) against three different databases, in
this specific order:</p>
<ol>
<li>The <code>postgres</code> database (<code>postInit</code> section)</li>
<li>The <code>template1</code> database (<code>postInitTemplate</code> section)</li>
<li>The application database (<code>postInitApplication</code> section)</li>
</ol>
<p>For each of these sections, CloudNativePG provides two ways to specify custom
queries, executed in the following order:</p>
<ul>
<li>As a list of SQL queries in the cluster's definition (<code>postInitSQL</code>,
<code>postInitTemplateSQL</code>, and <code>postInitApplicationSQL</code> stanzas)</li>
<li>As a list of Secrets and/or ConfigMaps, each containing a SQL script to be
executed (<code>postInitSQLRefs</code>, <code>postInitTemplateSQLRefs</code>, and
<code>postInitApplicationSQLRefs</code> stanzas). Secrets are processed before ConfigMaps.</li>
</ul>
<p>Objects in each list will be processed sequentially.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Use the <code>postInit</code>, <code>postInitTemplate</code>, and <code>postInitApplication</code> options
with extreme care, as queries are run as a superuser and can disrupt the entire
cluster. An error in any of those queries will interrupt the bootstrap phase,
leaving the cluster incomplete and requiring manual intervention.</p>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Ensure the existence of entries inside the ConfigMaps or Secrets specified
in <code>postInitSQLRefs</code>, <code>postInitTemplateSQLRefs</code>, and
<code>postInitApplicationSQLRefs</code>, otherwise the bootstrap will fail. Errors in any
of those SQL files will prevent the bootstrap phase from completing
successfully.</p>
</div>
<p>The following example runs a single SQL query as part of the <code>postInitSQL</code>
stanza:</p>
<pre><code class="language-yaml">apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cluster-example-initdb
spec:
instances: 3
bootstrap:
initdb:
database: app
owner: app
dataChecksums: true
localeCollate: 'en_US'
localeCType: 'en_US'
postInitSQL:
- CREATE DATABASE angus
storage:
size: 1Gi
</code></pre>
<p>The example below relies on <code>postInitApplicationSQLRefs</code> to specify a secret
and a ConfigMap containing the queries to run after the initialization on the
application database:</p>
<pre><code class="language-yaml">apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cluster-example-initdb
spec:
instances: 3
bootstrap:
initdb:
database: app
owner: app
postInitApplicationSQLRefs:
secretRefs:
- name: my-secret
key: secret.sql
configMapRefs:
- name: my-configmap
key: configmap.sql
storage:
size: 1Gi
</code></pre>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Within SQL scripts, each SQL statement is executed in a single exec on the
server according to the <a href="https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT">PostgreSQL semantics</a>.
Comments can be included, but internal commands like <code>psql</code> cannot.</p>
</div>
<h2 id="bootstrap-from-another-cluster">Bootstrap from another cluster</h2>
<p>CloudNativePG enables bootstrapping a cluster starting from
another one of the same major version.
This operation can be carried out either connecting directly to the source cluster via
streaming replication (<code>pg_basebackup</code>), or indirectly via an existing
physical <em>base backup</em> (<code>recovery</code>).</p>
<p>The source cluster must be defined in the <code>externalClusters</code> section, identified
by <code>name</code> (our recommendation is to use the same <code>name</code> of the origin cluster).</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>By default the <code>recovery</code> method strictly uses the <code>name</code> of the
cluster in the <code>externalClusters</code> section to locate the main folder
of the backup data within the object store, which is normally reserved
for the name of the server. You can specify a different one with the
<code>barmanObjectStore.serverName</code> property (by default assigned to the
value of <code>name</code> in the external cluster definition).</p>
</div>
<h3 id="bootstrap-from-a-backup-recovery">Bootstrap from a backup (<code>recovery</code>)</h3>
<p>Given the variety of backup methods and combinations of backup storage
options provided by the CloudNativePG operator for <code>recovery</code>, please refer
to the dedicated <a href="../recovery/">"Recovery" section</a> for detailed guidance on
each method.</p>
<h3 id="bootstrap-from-a-live-cluster-pg_basebackup">Bootstrap from a live cluster (<code>pg_basebackup</code>)</h3>
<p>The <code>pg_basebackup</code> bootstrap mode allows you to create a new cluster
(<em>target</em>) as an exact physical copy of an existing and <strong>binary-compatible</strong>
PostgreSQL instance (<em>source</em>) managed by CloudNativePG, using a valid
<em>streaming replication</em> connection. The source instance can either be a primary
or a standby PostgreSQL server. It’s crucial to thoroughly review the
requirements section below, as the pros and cons of PostgreSQL physical
replication fully apply.</p>
<p>The primary use cases for this method include:</p>
<ul>
<li>Reporting and business intelligence clusters that need to be regenerated
periodically (daily, weekly)</li>
<li>Test databases containing live data that require periodic regeneration
(daily, weekly, monthly) and anonymization</li>
<li>Rapid spin-up of a standalone replica cluster</li>
<li>Physical migrations of CloudNativePG clusters to different namespaces or
Kubernetes clusters</li>
</ul>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Avoid using this method, based on physical replication, to migrate an
existing PostgreSQL cluster outside of Kubernetes into CloudNativePG, unless you
are completely certain that all <a href="#requirements">requirements</a> are met and
the operation has been
thoroughly tested. The CloudNativePG community does not endorse this approach
for such use cases, and recommends using logical import instead. It is
exceedingly rare that all requirements for physical replication are met in a
way that seamlessly works with CloudNativePG.</p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>In its current implementation, this method clones the source PostgreSQL
instance, thereby creating a <em>snapshot</em>. Once the cloning process has finished,
the new cluster is immediately started.
Refer to <a href="#current-limitations">"Current limitations"</a> for more details.</p>
</div>
<p>Similar to the <code>recovery</code> bootstrap method, once the cloning operation is
complete, the operator takes full ownership of the target cluster, starting
from the first instance. This includes overriding certain configuration
parameters as required by CloudNativePG, resetting the superuser password,
creating the <code>streaming_replica</code> user, managing replicas, and more. The
resulting cluster operates independently from the source instance.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Configuring the network connection between the target and source instances
lies outside the scope of CloudNativePG documentation, as it depends heavily on
the specific context and environment.</p>
</div>
<p>The streaming replication client on the target instance, managed transparently
by <code>pg_basebackup</code>, can authenticate on the source instance using one of the
following methods:</p>
<ol>
<li><a href="#usernamepassword-authentication">Username/password</a></li>
<li><a href="#tls-certificate-authentication">TLS client certificate</a></li>
</ol>
<p>Both authentication methods are detailed below.</p>
<h4 id="requirements">Requirements</h4>
<p>The following requirements apply to the <code>pg_basebackup</code> bootstrap method:</p>
<ul>
<li>target and source must have the same hardware architecture</li>
<li>target and source must have the same major PostgreSQL version</li>
<li>target and source must have the same tablespaces</li>
<li>source must be configured with enough <code>max_wal_senders</code> to grant
access from the target for this one-off operation by providing at least
one <em>walsender</em> for the backup plus one for WAL streaming</li>
<li>the network between source and target must be configured to enable the target
instance to connect to the PostgreSQL port on the source instance</li>
<li>source must have a role with <code>REPLICATION LOGIN</code> privileges and must accept
connections from the target instance for this role in <code>pg_hba.conf</code>, preferably
via TLS (see <a href="#about-the-replication-user">"About the replication user"</a> below)</li>
<li>target must be able to successfully connect to the source PostgreSQL instance
using a role with <code>REPLICATION LOGIN</code> privileges</li>
</ul>
<div class="admonition seealso">
<p class="admonition-title">Seealso</p>
<p>For further information, please refer to the
<a href="https://www.postgresql.org/docs/current/warm-standby.html#STANDBY-PLANNING">"Planning" section for Warm Standby</a>,
the
<a href="https://www.postgresql.org/docs/current/app-pgbasebackup.html"><code>pg_basebackup</code> page</a>
and the
<a href="https://www.postgresql.org/docs/current/high-availability.html">"High Availability, Load Balancing, and Replication" chapter</a>
in the PostgreSQL documentation.</p>
</div>
<h4 id="about-the-replication-user">About the replication user</h4>
<p>As explained in the requirements section, you need to have a user
with either the <code>SUPERUSER</code> or, preferably, just the <code>REPLICATION</code>
privilege in the source instance.</p>
<p>If the source database is created with CloudNativePG, you
can reuse the <code>streaming_replica</code> user and take advantage of client
TLS certificates authentication (which, by default, is the only allowed
connection method for <code>streaming_replica</code>).</p>
<p>For all other cases, including outside Kubernetes, please verify that
you already have a user with the <code>REPLICATION</code> privilege, or create
a new one by following the instructions below.</p>
<p>As <code>postgres</code> user on the source system, please run:</p>
<pre><code class="language-console">createuser -P --replication streaming_replica
</code></pre>
<p>Enter the password at the prompt and save it for later, as you
will need to add it to a secret in the target instance.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Although the name is not important, we will use <code>streaming_replica</code>
for the sake of simplicity. Feel free to change it as you like,
provided you adapt the instructions in the following sections.</p>
</div>
<h4 id="usernamepassword-authentication">Username/Password authentication</h4>
<p>The first authentication method supported by CloudNativePG
with the <code>pg_basebackup</code> bootstrap is based on username and password matching.</p>
<p>Make sure you have the following information before you start the procedure:</p>
<ul>
<li>location of the source instance, identified by a hostname or an IP address
and a TCP port</li>
<li>replication username (<code>streaming_replica</code> for simplicity)</li>
<li>password</li>
</ul>
<p>You might need to add a line similar to the following to the <code>pg_hba.conf</code>
file on the source PostgreSQL instance:</p>
<pre><code># A more restrictive rule for TLS and IP of origin is recommended
host replication streaming_replica all md5
</code></pre>
<p>The following manifest creates a new PostgreSQL 17.5 cluster,
called <code>target-db</code>, using the <code>pg_basebackup</code> bootstrap method
to clone an external PostgreSQL cluster defined as <code>source-db</code>
(in the <code>externalClusters</code> array). As you can see, the <code>source-db</code>
definition points to the <code>source-db.foo.com</code> host and connects as
the <code>streaming_replica</code> user, whose password is stored in the
<code>password</code> key of the <code>source-db-replica-user</code> secret.</p>
<pre><code class="language-yaml">apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: target-db
spec:
instances: 3
imageName: ghcr.io/cloudnative-pg/postgresql:17.5
bootstrap:
pg_basebackup:
source: source-db
storage:
size: 1Gi
externalClusters:
- name: source-db
connectionParameters:
host: source-db.foo.com
user: streaming_replica
password:
name: source-db-replica-user
key: password
</code></pre>
<p>All the requirements must be met for the clone operation to work, including
the same PostgreSQL version (in our case 17.5).</p>
<h4 id="tls-certificate-authentication">TLS certificate authentication</h4>
<p>The second authentication method supported by CloudNativePG
with the <code>pg_basebackup</code> bootstrap is based on TLS client certificates.
This is the recommended approach from a security standpoint.</p>
<p>The following example clones an existing PostgreSQL cluster (<code>cluster-example</code>)
in the same Kubernetes cluster.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This example can be easily adapted to cover an instance that resides
outside the Kubernetes cluster.</p>
</div>
<p>The manifest defines a new PostgreSQL 17.5 cluster called <code>cluster-clone-tls</code>,
which is bootstrapped using the <code>pg_basebackup</code> method from the <code>cluster-example</code>
external cluster. The host is identified by the read/write service
in the same cluster, while the <code>streaming_replica</code> user is authenticated
thanks to the provided keys, certificate, and certification authority
information (respectively in the <code>cluster-example-replication</code> and
<code>cluster-example-ca</code> secrets).</p>
<pre><code class="language-yaml">apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cluster-clone-tls
spec:
instances: 3
imageName: ghcr.io/cloudnative-pg/postgresql:17.5
bootstrap:
pg_basebackup:
source: cluster-example
storage:
size: 1Gi
externalClusters:
- name: cluster-example
connectionParameters:
host: cluster-example-rw.default.svc
user: streaming_replica
sslmode: verify-full
sslKey:
name: cluster-example-replication
key: tls.key
sslCert:
name: cluster-example-replication
key: tls.crt
sslRootCert:
name: cluster-example-ca
key: ca.crt
</code></pre>
<h4 id="configure-the-application-database">Configure the application database</h4>
<p>We also support to configure the application database for cluster which bootstrap
from a live cluster, just like the case of <code>initdb</code> and <code>recovery</code> bootstrap method.
If the new cluster is created as a replica cluster (with replica mode enabled), application
database configuration will be skipped.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>While the <code>Cluster</code> is in recovery mode, no changes to the database,
including the catalog, are permitted. This restriction includes any role