Skip to main content
Version: Preview

Create and connect to a PostgreSQL cluster

This tutorial shows how to create and connect to a PostgreSQL cluster.

Create a PostgreSQL cluster

Before you start

  • Install kbcli if you want to manage the PostgreSQL cluster by kbcli.

  • Install KubeBlocks by kbcli or by Helm.

  • Make sure the PostgreSQL Addon is enabled. The PostgreSQL Addon is installed and enabled by KubeBlocks by default. But if you disable it when installing KubeBlocks, enable it first.

    kbcli addon list
    >
    NAME TYPE STATUS EXTRAS AUTO-INSTALL
    ...
    postgresql Helm Enabled true
    ...
  • View all the database types and versions available for creating a cluster.

    kbcli clusterdefinition list
    kbcli clusterversion list
  • To keep things isolated, create a separate namespace called demo throughout this tutorial.

    kubectl create namespace demo

Create a cluster

KubeBlocks supports creating two types of PostgreSQL clusters: Standalone and Replication Cluster. Standalone only supports one replica and can be used in scenarios with lower requirements for availability. For scenarios with high availability requirements, it is recommended to create a Replication Cluster, which creates a cluster with a Replication Cluster to support automatic failover. To ensure high availability, Primary and Secondary are distributed on different nodes by default.

  1. Create a PostgreSQL cluster.

    Here is an example of creating a Standalone.

    kbcli cluster create postgresql mycluster -n demo

    kbcli provides various options for you to customize your cluster specifications, such as setting cluster version, termination policy, CPU, and memory. You can view these options by adding --help or -h flag.

    kbcli cluster create postgresql --help
    kbcli cluster create postgresql -h

    For example, you can create a Replication Cluster with the --replicas flag.

    kbcli cluster create postgresql mycluster --replicas=2 -n demo

    If you only have one node for deploying a Replication Cluster, set the --topology-keys as null when creating a Replication Cluster. But you should note that for a production environment, it is not recommended to deploy all replicas on one node, which may decrease the cluster availability.

    kbcli cluster create postgresql mycluster --replicas=2 --availability-policy='none' -n demo
  2. Verify whether this cluster is created successfully.

    kbcli cluster list -n demo
    >
    NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
    mycluster demo postgresql postgresql-14.8.0 Delete Running Sep 28,2024 16:47 UTC+0800

Connect to a PostgreSQL Cluster

kbcli cluster connect mycluster  --namespace demo

For the detailed database connection guide, refer to Connect database.