Skip to content

2 Connect to the PostgreSQL cluster

When the installation is done, we can connect to the cluster.

The pgBouncer component of Percona Distribution for PostgreSQL provides the point of entry to the PostgreSQL cluster. We will use the pgBouncer URI to connect.

The pgBouncer URI is stored in the Secret object, which the Operator generates during the installation.

To connect to PostgreSQL, do the following:

  1. List the Secrets objects

    $ kubectl get secrets -n <namespace>
    

    The Secrets object we target is named as <cluster_name>-pguser-<cluster_name>. The <cluster_name> value is the name of your Percona Distribution for PostgreSQL Cluster. The default variant is:

    cluster1-pguser-cluster1

    cluster1-pg-db-pguser-cluster1-pg-db

  2. Retrieve the pgBouncer URI from your secret, decode and pass it as the PGBOUNCER_URI environment variable. Replace the <secret>, <namespace> placeholders with your Secret object and namespace accordingly:

    $ PGBOUNCER_URI=$(kubectl get secret <secret> --namespace <namespace> -o jsonpath='{.data.pgbouncer-uri}' | base64 --decode)
    

    The following example shows how to pass the pgBouncer URI from the default Secret object cluster1-pguser-cluster1:

    $ PGBOUNCER_URI=$(kubectl get secret cluster1-pguser-cluster1 --namespace <namespace> -o jsonpath='{.data.pgbouncer-uri}' | base64 --decode)
    
  3. Create a Pod where you start a container with Percona Distribution for PostgreSQL and connect to the database. The following command does it, naming the Pod pg-client and connects you to the cluster1 database:

    $ kubectl run -i --rm --tty pg-client --image=perconalab/percona-distribution-postgresql:16 --restart=Never -- psql $PGBOUNCER_URI
    

    It may take some time to create the Pod and connect to the database. As the result, you should see the following sample output:

    Expected output
    psql (16.4)
    SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
    Type "help" for help.
    cluster1=>
    

Congratulations! You have connected to your PostgreSQL cluster.

Next steps

Insert testing data

Get expert help

If you need assistance, visit the community forum for comprehensive and free database knowledge, or contact our Percona Database Experts for professional support and services. Join K8S Squad to benefit from early access to features and “ask me anything” sessions with the Experts.


Last update: 2024-10-08