Skip to main content

Create a Postgres container

If you need a quick, ephemeral database for testing/development, you can create a pod in your namespace running postgres in a container.

The Bitnami PostgreSQL helm chart is the easiest way to get started with PostgreSQL on Kubernetes. This chart bootstraps a PostgreSQL deployment on a Kubernetes cluster using the Helm package manager.

Note: Using a postgres container is only recommended for ephemeral testing/development purposes. For any production, or long-lived development/testing environments, we strongly recommend using the AWS RDS managed database service.

Pre-requisites

Set up

First copy this values.yaml file to your working directory.

wget https://raw.githubusercontent.com/helm/charts/master/stable/postgresql/values.yaml

You should edit the file to set your own values for:

  • postgresqlUsername
  • postgresqlPassword
  • postgresqlDatabase

The postgres helm chart is in the “bitnami” chart repository, so you need to add that:

helm repo add bitnami https://charts.bitnami.com/bitnami

Then install the edited chart like this:

helm install mydb bitnami/postgresql -f values.yaml --namespace <your namespace>

Change mydb to whatever name you want your pod to have.

Check that the PostgreSQL Helm Chart is deployed successful:

kubectl get pods --namespace <your namespace>

If the Installation was successful you should see something similar to this:

NAME                        READY     STATUS    RESTARTS   AGE
mydb-postgresql-0           1/1       Running   0          39m

You should have a postgres pod with the status running. You can also check the logs of the PostgreSQL pod:

kubectl --namespace <your namespace> logs mydb-postgresql-0

If the PostgreSQL setup was successful, the last line should be something like:

2020-08-07 14:32:58.158 GMT [1] LOG:  database system is ready to accept connections

Accessing your PostgreSQL DB

PostgreSQL can be accessed via port 5432 on the following DNS name from within the cluster:

mydb-postgresql.<your namespace>.svc.cluster.local - Read/Write connection

The postgresqlPassword you have set will be stored as a secret in your namespace, to get the password for the postgres database user, run:

export POSTGRES_PASSWORD=$(kubectl get secret --namespace <your namespace> mydb-postgresql -o jsonpath="{.data.postgresql-password}" | base64 --decode)

To connect to your database from outside the cluster execute the following commands:

kubectl port-forward --namespace <your namespace> svc/mydb-postgresql 5432:5432 &

PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -d my-database -p 5432

Cleaning up

When you have finished with your postgres instance, you can get rid of it like this:

helm uninstall mydb --namespace <your namespace>

If you used something other than mydb as the name of your installation, you will need to use that name instead.

This page was last reviewed on 23 February 2024. It needs to be reviewed again on 23 August 2024 by the page owner #cloud-platform .
This page was set to be reviewed before 23 August 2024 by the page owner #cloud-platform. This might mean the content is out of date.