Don’t know how backup works? Check tutorial on Instant Backup.

Initialize PostgreSQL with Snapshot

KubeDB supports PostgreSQL database initialization. This tutorial will show you how to use KubeDB to initialize a PostgreSQL database with existing snapshot data.

Before You Begin

At first, you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using minikube.

Now, install KubeDB cli on your workstation and KubeDB operator in your cluster following the steps here.

To keep things isolated, this tutorial uses a separate namespace called demo throughout this tutorial.

$ kubectl create ns demo
namespace "demo" created

$ kubectl get ns demo
demo    Active  5s

Prepare Snapshot

We need a Snapshot to perform this initialization. If you don’t have a Snapshot already, create one by following the tutorial here.

If you have changed the name of either namespace or snapshot object, please modify the YAMLs used in this tutorial accordingly.

Note: Yaml files used in this tutorial are stored in docs/examples/postgres folder in GitHub repository kubedb/cli.

Create PostgreSQL with Snapshot source

You have to specify the Snapshot name and namespace in the spec.init.snapshotSource field of your new Postgres object.

Below is the YAML for PostgreSQL object created in this tutorial.

kind: Postgres
  name: recovered-postgres
  namespace: demo
  version: "9.6-v1"
    secretName: script-postgres-auth
    storageClassName: "standard"
    - ReadWriteOnce
        storage: 50Mi
      name: instant-snapshot
      namespace: demo


  • spec.init.snapshotSource specifies Snapshot object information to be used in this initialization process.
    • refers to a Snapshot object name.
    • snapshotSource.namespace refers to a Snapshot object namespace.

Snapshot instant-snapshot in demo namespace belongs to Postgres script-postgres:

$ kubectl get snap -n demo instant-snapshot
NAME               DATABASENAME      STATUS      AGE
instant-snapshot   script-postgres   Succeeded   56s

Note: Postgres recovered-postgres must have same superuser credentials as Postgres script-postgres.

Now, create the Postgres object.

$ kubedb create -f created

When PostgreSQL database is ready, KubeDB operator launches a Kubernetes Job to initialize this database using the data from Snapshot instant-snapshot.

As a final step of initialization, KubeDB Job controller adds annotation in initialized Postgres object. This prevents further invocation of initialization process.

$ kubedb describe pg -n demo recovered-postgres -S=false -W=false
Name:           recovered-postgres
Namespace:      demo
StartTimestamp: Thu, 08 Feb 2018 17:23:21 +0600
Status:         Running
    namespace:  demo
    name:       instant-snapshot
  StorageClass: standard
  Capacity:     50Mi
  Access Modes: RWO
StatefulSet:    recovered-postgres
Service:        recovered-postgres, recovered-postgres-replicas
Secrets:        script-postgres-auth

  Type      Pod                    StartTime                       Phase
  ----      ---                    ---------                       -----
  primary   recovered-postgres-0   2018-02-08 17:23:26 +0600 +06   Running

No Snapshots.

  FirstSeen   LastSeen   Count     From                Type       Reason               Message
  ---------   --------   -----     ----                --------   ------               -------
  20s         20s        1         Postgres operator   Normal     Successful           Successfully patched StatefulSet
  20s         20s        1         Postgres operator   Normal     Successful           Successfully patched Postgres
  24s         24s        1         Job Controller      Normal     SuccessfulSnapshot   Successfully completed initialization
  31s         31s        1         Postgres operator   Normal     Initializing         Initializing from Snapshot: "instant-snapshot"
  34s         34s        1         Postgres operator   Normal     Successful           Successfully created StatefulSet
  34s         34s        1         Postgres operator   Normal     Successful           Successfully created Postgres
  35s         35s        1         Postgres operator   Normal     Successful           Successfully created Service
  35s         35s        1         Postgres operator   Normal     Successful           Successfully created Service

Verify Initialization

Now, let’s connect to our Postgres recovered-postgres using pgAdmin we have installed in quickstart tutorial to verify that the database has been successfully initialized.

Connection Information:

  • Host name/address: you can use any of these
    • Service: recovered-postgres.demo
    • Pod IP: ($ kubectl get pods recovered-postgres-0 -n demo -o yaml | grep podIP)
  • Port: 5432
  • Maintenance database: postgres

  • Username: Run following command to get username,

  $ kubectl get secrets -n demo script-postgres-auth -o jsonpath='{.data.\POSTGRES_USER}' | base64 -d
  • Password: Run the following command to get password,
  $ kubectl get secrets -n demo script-postgres-auth -o jsonpath='{.data.\POSTGRES_PASSWORD}' | base64 -d

In PostgreSQL, run following query to check pg_catalog.pg_tables to confirm initialization.

select * from pg_catalog.pg_tables where schemaname = 'data';
schemaname tablename tableowner hasindexes hasrules hastriggers rowsecurity
data dashboard postgres t f f f

We can see TABLE dashboard in data Schema which is created for initialization.


Cleaning up

To cleanup the Kubernetes resources created by this tutorial, run:

$ kubectl patch -n demo pg/script-postgres pg/recovered-postgres -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
$ kubectl delete -n demo pg/script-postgres pg/recovered-postgres

$ kubectl delete ns demo

Next Steps

Subscribe to our free technical newsletter!

Join thousands of subscribers and stay up-to-date on AppsCode.