1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-05 21:46:22 +01:00
Files
openshift-docs/modules/sbo-creating-a-postgresql-database-instance.adoc
2021-10-08 20:04:57 +00:00

122 lines
4.1 KiB
Plaintext

[id="sbo-creating-a-postgresql-database-instance_{context}"]
= Creating a PostgreSQL database instance
To create a PostgreSQL database instance, you must create a `PostgresCluster` custom resource (CR) and configure the database.
[discrete]
.Procedure
. Create the `PostgresCluster` CR and the `my-postgresql` namespace by running the following command in shell:
+
----
$ oc apply -f - << EOD
---
apiVersion: v1
kind: Namespace
metadata:
name: my-postgresql
---
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: hippo
namespace: my-postgresql
annotations:
service.binding: 'path={.metadata.annotations.dbsecret},objectType=Secret'
dbsecret: hippo-pguser-hippo
proxy: hippo-pgbouncer
type: postgresql
service.binding/database: path={.metadata.name}
service.binding/port: path={.spec.port}
service.binding/username: path={.metadata.name}
service.binding/host: path={.metadata.annotations.proxy}
service.binding/type: path={.metadata.annotations.type}
spec:
image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres-ha:centos8-13.4-0
postgresVersion: 13
instances:
- name: instance1
dataVolumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
backups:
pgbackrest:
image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.33-2
repos:
- name: repo1
volume:
volumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
- name: repo2
volume:
volumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
proxy:
pgBouncer:
image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbouncer:centos8-1.15-2
EOD
----
+
The annotations added in this `PostgresCluster` CR help in enabling the service binding connection and trigger the Operator reconciliation.
+
The output verifies that the database instance is created:
+
.Example output
----
namespace/my-postgresql configured
postgrescluster.postgres-operator.crunchydata.com/hippo created
----
. After you have created the database instance, ensure that all the pods in `my-postgresql` namespace are running (it will take a few minutes):
+
----
$ oc get pods -n my-postgresql
----
+
The output verifies that the database is created:
+
.Example output
----
NAME READY STATUS RESTARTS AGE
hippo-backup-6th6--1-28849 0/1 Completed 0 1m
hippo-instance1-sl4r-0 2/2 Running 0 2m
hippo-pgbouncer-8454474bc7-lhcn9 2/2 Running 0 2m
----
+
The new database is empty at this stage. You can set its schema and project a sample data set to interact with the sample application.
. Initialize the database with the schema and sample data. To do so, use the following custom shell script by copying the code into the shell and running it:
+
----
$ cat << EOD | bash
#!/bin/bash
export pgo_cluster_name=hippo
export cluster_namespace=my-postgresql
export pgo_cluster_username=hippo
export PGPASSWORD=\$(oc -n "\${cluster_namespace}" get secrets \
"\${pgo_cluster_name}-pguser-\${pgo_cluster_username}" -o "jsonpath={.data['password']}" | base64 -d)
nohup oc -n \${cluster_namespace} port-forward svc/hippo-pgbouncer 5432:5432 &
sleep 5
curl -LO https://raw.githubusercontent.com/spring-petclinic/spring-petclinic-rest/master/src/main/resources/db/postgresql/initDB.sql
psql -h localhost -U "\${pgo_cluster_username}" "\${pgo_cluster_name}" -f initDB.sql
curl -LO https://raw.githubusercontent.com/spring-petclinic/spring-petclinic-rest/master/src/main/resources/db/postgresql/populateDB.sql
psql -h localhost -U "\${pgo_cluster_username}" "\${pgo_cluster_name}" -f populateDB.sql
EOD
----
+
The output in the terminal shows you that the database is being configured for the application.
After the database is configured, you can deploy the sample application and connect it to the database service.