I have deployed postgresql instance in k8s using helm and creating a database using values.yaml. Now How to create table.
global:
postgresql:
auth:
# Added below credentials for testing
postgresPassword: "postgresqladmin"
username: "admin"
password: "admin"
database: "postgresqlDB"
containerPorts:
postgresql: 5432
image:
registry: docker.io
repository: bitnami/postgresql
tag: 15.4.0-debian-11-r10
digest: ""
pullPolicy: IfNotPresent
debug: false
Here is the command to install:
helm upgrade --install postgresql -f values.yaml bitnami/postgresql
And here is the command to access db(I am using suggested command after install)
kubectl run postgresql-client --rm --tty -i --restart='Never' --image bitnami/postgresql --env="admin" --command -- psql --host postgresql -U admin -d postgresqlDB -p 5432
3
Answers
You have two options.
1. Access container
You can access to your database after it has been mounted like this, and then execute your sql script.
kubectl run -it –rm –image=bitnami/postgresql:15.4.0-debian-11-r10 –restart=Never postgresql-client — psql -h postgresql -U admin -d postgresqlDB
2. Include a script to your deployment :
You create an SQL script that create your table myscript.sql :
Create a ConfigMap, a YAML file (e.g., configmap-create-my-table.yaml) with the following content:
Apply this to your cluster
kubectl apply -f configmap-create-table.yaml
Adapt your helm chart :
And then upgrade it :
helm upgrade --install postgresql -f values.yaml bitnami/postgresql
In order to create table after you have created database using values.yaml, try these steps;
First you have to connect to postgreSQL using client:
Now connect to your database inside the postgreSQL client pod;
NOw using simple SQL Commands, you can create tables;
After successfully creating the tables, you can exit the PostgreSQL client using;
ostgreSQL
Hope it works:)
once you start the database using –
You’d need to connect to the instance via –
You will be prompted for your password, which you specified in your values.yaml file (in your case, it’s "postgresqladmin").
After this, you’ll see the cursor change to postgresql / the username you specified, this indicates that you can now access the database using query language.
To create a table from here on, you can simply type in
and it will create these columns with the specified data types and the primary key. You can exit the client using
q
and exit the container futher usingexit
.