Running pgAdmin to Manage a PostgreSQL Cluster on Kubernetes
Tags: percona, kubernetes, postgres, database
Published: 2024-06-26 18:14:16

Let’s say you need to do something in PostgreSQL in Kubernetes, and it is inconvenient to work with the database in the terminal, or you need to become more familiar with PostgreSQL or SQL commands. In that case, this article comes in handy.

I explain how to run pgAdmin in a Kubernetes cluster to manage PostgreSQL databases deployed in that cluster. This is useful if your PostgreSQL cluster does not have external access and is only available inside a Kubernetes cluster.

[pgAdmin](https://www.pgadmin.org/) is a popular open source tool for managing Postgres databases. It is convenient for creating databases, schemas, tables, viewing data, executing SQL queries, and much more in a user-friendly web interface.

![pgAdmin PostgreSQL Kubernetes Welcome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgcasz62axh0kazua408.png)

## Running pgAdmin

We need one file that contains deployment and service resources to run pgAdmin in k8s. Let's call it pgadmin.yaml.

pgadmin.yaml
```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgadmin-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgadmin
  template:
    metadata:
      labels:
        app: pgadmin
    spec:
      containers:
        - name: pgadmin
          image: dpage/pgadmin4
          ports:
            - containerPort: 80
          env:
            - name: PGADMIN_DEFAULT_EMAIL
              value: admin@example.com
            - name: PGADMIN_DEFAULT_PASSWORD
              value: admin
---
apiVersion: v1
kind: Service
metadata:
  name: pgadmin-service
spec:
  selector:
    app: pgadmin
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
```

Note that the middle of the file contains the login and password for logging into the pgAdmin panel.

```
          env:
            - name: PGADMIN_DEFAULT_EMAIL
              value: admin@example.com
            - name: PGADMIN_DEFAULT_PASSWORD
              value: admin
```

We need to deploy pgAdmin using this file by running the command.

`kubectl apply -f pgadmin.yaml -n <namespace>`

Let's print out the list of pods in our cluster to get the name pgadmin pod.

`kubectl get pods -n <namespace>`

Now, we just need to run port forwarding for the pgAdmin pod on a free port that will open in the browser. I use port 5050.

`kubectl port-forward pgadmin-deployment-***-*** 5050:80 -n <namespace>`

![pgAdmin deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kq7b1f39olia5d2c0u0.jpg)

I can open localhost:5050 in a browser now.

After that, you only need to add a connection to Postgres by clicking Add New Server.

![pgAdmin Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0d6fesmyne41jsk1rvd.png)

I used the access data that [Percona Everest](https://percona.community/projects/everest) provided me for the Postgres cluster created using it.

![Percona Everest PostgreSQL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fsiyccostcw7h8zn0vzt.png)

![pgAdmin Add New Server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd9w4w94w34cya04wrez.png)

Congratulations! We can manage Postgres using the pgAdmin interface or SQL queries. Viewing databases, schemas, and tables and writing SQL queries has become very convenient.

![pgAdmin k8s SQL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cg0xhu6qeww5b08krzd6.png)

![pgAdmin k8s Select](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhm5a8847cex6k25lzy5.png)

## Remove pgAdmin

Once you are done with pgAdmin and PostgreSQL, all you need to do is to

1. Stop port-forwarding by simply pressing CTRL + C in the terminal. 

2. Remove pgAdmin Deployment and Service by running the command. 

![Remove pgAdmin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzymj9qygh0su2l6p74e.png)

## About the PostgreSQL cluster on Kubernetes used in the article 

In this post, I used the Postgres cluster with three nodes created on Google Kubernetes Engine (GKE) using [Percona Everest](https://percona.community/projects/everest).

Percona Everest is an open source platform that allows you to provision and manage database clusters. I recently wrote about Percona Everest in [A New Way to Provision Databases on Kubernetes](https://dev.to/dbazhenov/a-new-way-to-provision-databases-on-kubernetes-126h). You do not need to worry about PostgreSQL cluster installation and configuration, backups, recovery, or scaling; you can do it in the interface. It enables multi-database and multi-cluster configurations and can be deployed on any Kubernetes infrastructure in the cloud or on-premises.

Documentation: [Create Kubernetes cluster on Google Kubernetes Engine (GKE)](https://docs.percona.com/everest/quickstart-guide/gke.html)

You can use any other cloud or create a local Kubernetes cluster using [minikube](https://minikube.sigs.k8s.io/docs/), [k3d](https://k3d.io) or [kind](https://kind.sigs.k8s.io/).