CYBERTEC PostgreSQL Logo

Exploration: CNPG Kubectl Plugin

09.2025
Category: 

Introduction

We have explored how to create cluster, take backups, connect to the cluster and run psql commands in our CNPG series. However, one might feel overwhelmed because of those day - to - day operations. That is why CNPG provides a kubectl plugin. CloudNativePG' s plugin enriches kubectl with a set of PostgreSQL - focused commands, making easier to inspect clusters, trigger backups, promote a new instance, run pgbench and run psql commands without leaving existing terminal. Even though it is a pretty simple and straightforward topic, but it is important for completeness of our CNPG series.

Plugin Installation

There are different ways to install the plugin, but I found installing using the script easiest for me:

Commands

The pluging provides a variety of commands. "--help" is useful to get help for exploring available commands. For example;

If a help is needed for a specific command then;

Install

This command is used to generate a yaml manifest that is used for the installation of the operator. In this way, we can modify the default settings of the operator such as # replica and installation namespace.

Status

Status command show us the current status of respective cluster:

In this ouput we see about general information about the cluster, backup status, current WAL and replication status. Using verbose flag, "-v" or "--verbose" we can also display tablescpaces, managed roles and unmanaged replication slots.

Backup

If you suddenly need to take a backup of your CNPG cluster, the backup command provides a quick and straightforward way to do so:

Let' s check the backup status:

Opsie, we have an error! Once we check the details of the backup we know how to deal with it:

The default backup method of CNPG plugin is "barmanObjectStore". However, our backup method is plugin as we explained in our previous post. So, we need to specify the method we are going to use:

psql

This command provides a shortcut: instead of manually entering a pod and running psql, you can open a psql terminal directly:

It also accepts the same arguments as psql:

pgadmin4

Yes, you see correct. You can run a pgadmin4 pod to manage and query your database if you prefer a GUI over the terminal:

As it can be seen, the plugin creates a config map, secret, pod and service for pgadmin4. All we need to is run port forwarding command and then open "localhost:8080" in your favourite browser:

As we can see, the plugin creates a ConfigMap, Secret, Pod, and Service for pgAdmin4. All we need to do next is run a port-forwarding command and then open localhost:8080 in the browser:

Promote

In order to perform a switchover, use the promote command :

As shown in the status command section the primary pod was the first one, let' s check whether the promotion is sucessful:

The logs of the pod also confirms the switchover:

Publication

This command is used to create or remote a logical replication publication. A key point to remember is that when creating a publication on a remote cluster, an external cluster must be defined in the cluster specification. For example;

Because I define source - cluster in my cluster-example-backup, now I could create a publication on the source - cluster:

However, if we want to create a publication on local cluster in Kubernetes then all we need to is remove --external-cluster flag:

Dropping the publication is also simple as it seems:

Subscription

Like in the publication, subscripton is used to create or remove a logical replication subscripton.

With this command we created a subscription on local cluster-example-backup cluster using a publication created on our remote cluster which is source-cluster.

To drop the subscription:

Sync-sequences

The sync-sequences command simplifies database migrations. If new rows have been inserted into the source database since the last schema export/import, sequence synchronization becomes necessary. Note that this command depends on an existing subscription.

Conclusion

The CloudNativePG kubectl plugin simplifies day-to-day PostgreSQL cluster operations by providing a unified command set in Kubernetes. Instead of switching contexts or writing complex manifests, you can quickly check cluster health, perform backups, manage switchover operations, access psql, deploy pgAdmin4, and configure logical replication with just a few commands. While each task could be performed manually, the plugin brings consistency, convenience, and reduced operational overhead.

Leave a Reply

Your email address will not be published. Required fields are marked *

CYBERTEC Logo white
Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2025
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram