Deploy PostgreSQL with Portworx
PostgreSQL is an open-source relational database management system known for its robustness, SQL compliance, and extensibility. Learn how to deploy and operate PostgreSQL with Portworx Enterprise on Kubernetes. Portworx provides a resilient, high-performance persistent storage layer that ensures data durability, fault tolerance, and seamless scaling for PostgreSQL database workloads.
Postgres serves as a core data store for many applications that require strong consistency, transactional integrity, and advanced querying capabilities. It can be configured to securely store and manage structured data for a wide range of workloads.
To deploy PostgreSQL with Portworx Enterprise, complete the following collection of tasks:
- Create a StorageClass for dynamic volume provisioning with Portworx Enterprise.
- Create a PVC to request a persistent storage.
- Deploy PostgreSQL using Stork.
Create a StorageClass
-
Define a storageclass
px-postgres-scand save it in a filepx-postgres-sc.yaml.kind: StorageClassapiVersion: storage.k8s.io/v1metadata:name: px-postgres-scprovisioner: pxd.portworx.comparameters:repl: "2"allowVolumeExpansion: trueNote the following about this
StorageClass:- The
provisionerparameter is set topxd.portworx.com. - Two replicas of each volume will be created
- The
-
Apply the spec by entering the following command:
kubectl apply -f px-postgres-sc.yamlstorageclass.storage.k8s.io/px-postgres-sc created
Create a PVC
-
Define a PVC and save it in a file
px-postgres-vol.yaml.kind: PersistentVolumeClaimapiVersion: v1metadata:name: postgres-dataspec:storageClassName: px-postgres-scaccessModes:- ReadWriteOnceresources:requests:storage: 1GiThis PVC references the
px-postgres-scstorage class defined in the Create a StorageClass section. Kubernetes will automatically create a new PVC for each replica. -
Apply the spec by entering the following command:
kubectl apply -f px-postgres-vol.yamlpersistentvolumeclaim/postgres-data created
Deploy PostgreSQL using Stork
-
Define a deployment that uses Stork as a scheduler and save it in a file
px-postgres-app.yaml.apiVersion: apps/v1kind: Deploymentmetadata:name: postgresspec:selector:matchLabels:app: postgresstrategy:rollingUpdate:maxSurge: 1maxUnavailable: 1type: RollingUpdatereplicas: 1template:metadata:labels:app: postgresspec:schedulerName: storkaffinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: px/enabledoperator: NotInvalues:- "false"containers:- name: postgresimage: postgres:18.1imagePullPolicy: "IfNotPresent"ports:- containerPort: 5432env:- name: POSTGRES_USERvalue: pgbench- name: POSTGRES_PASSWORDvalue: superpostgres- name: PGBENCH_PASSWORDvalue: superpostgres- name: PGDATAvalue: /var/lib/postgresql/data/pgdatavolumeMounts:- mountPath: /var/lib/postgresql/dataname: postgredbvolumes:- name: postgredbpersistentVolumeClaim:claimName: postgres-dataNote the following:
- Specifies Stork as scheduler (
schedulerName: stork) - Sets the following environment variables:
- POSTGRES_USER (defines the superuser)
- POSTGRES_PASSWORD (specifies the superuser password)
- PGDATA (configures the location for the database files)
- References the
postgres-dataPVC defined in the Create a PVC section.
- Specifies Stork as scheduler (
-
Apply the spec by entering the following command:
kubectl apply -f px-postgres-app.yamldeployment.apps/postgres created
Verify your PostgreSQL installation
-
Enter the following
kubectl getcommand to list your storage classes:kubectl get scNAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGEpx-postgres-sc pxd.portworx.com Delete Immediate true 4m41sIn the above example output, note that the provisioner is set to
pxd.portworx.com -
Enter the
kubectl get pvccommand to verify that the PVC is bound to a volume:kubectl get pvcNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGEpostgres-data Bound pvc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxb415 1Gi RWO px-postgres-sc <unset> 3m34s -
Use the
kubectl get podscommand to verify the status of the PostgreSQL pod:kubectl get podNAME READY STATUS RESTARTS AGEpostgres-687bbdd6d9-zrdqz 1/1 Running 0 2mMake a note of the name of the pod. You'll need it in the next step.
-
Enter the following
kubectl execcommand, specifying your own pod name, to open a shell session into your pod. This example opens thepostgres-86cb8587c4-l9r48pod:kubectl exec -it postgres-86cb8587c4-l9r48 -- bashroot@postgres-687bbdd6d9-zrdqz:/# -
Start the PostgreSQL interactive shell. Use the
-Uflag to connect as thepgbenchuser:root@postgres-86cb8587c4-l9r48:/# psql -U pgbenchpsql (9.5.10)Type "help" for help. -
List your databases:
pgbench=# \lName | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges-----------+---------+----------+-----------------+------------+------------+--------+-----------+---------------------pgbench | pgbench | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |postgres | pgbench | UTF8 | libc | en_US.utf8 | en_US.utf8 | | |template0 | pgbench | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/pgbench +| | | | | | | | pgbench=CTc/pgbenchtemplate1 | pgbench | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/pgbench +| | | | | | | | pgbench=CTc/pgbench(4 rows) -
Exit the PostgreSQL interactive shell:
pgbench=# \qroot@postgres-687bbdd6d9-zrdqz:/# exitexit