Reputation: 1036
I have used this link to deploy Postgres on Kubernetes.
below is the configmap :
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
labels:
app: postgres
data:
POSTGRES_DB: postgresdb
POSTGRES_USER: postgresadmin
POSTGRES_PASSWORD: admin123
and deployment :
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:10.4
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: postgres-config
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim
I hope, a simple configuration at configmap or deployment yaml will help to create a new schema on postgres db.
Upvotes: 0
Views: 4566
Reputation: 864
For your use-case, it is better to setup PostgreSQL using Helm charts, for e.g.:
helm install stable/postgresql \
--set global.postgresql.postgresqlDatabase=postgresdb \
--set global.postgresql.postgresqlUsername=postgresadmin \
--set global.postgresql.postgresqlPassword=admin123 \
--set global.postgresql.servicePort=5432 \
--set initdbScripts."init\.sql"="CREATE SCHEMA IF NOT EXISTS key;"
Upvotes: 3
Reputation: 3069
You can use Kubernetes postStart lifecycle hook to create your schema on given Postgres DB. Just update your deployment file with the lifecycle block.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: postgres
name: postgres
spec:
containers:
- image: postgres:10.4
name: postgres
resources: {}
lifecycle:
postStart:
exec:
command: ["/bin/bash","-c","sleep 20 && PGPASSWORD=$POSTGRES_PASSWORD psql $POSTGRES_DB -U $POSTGRES_USER -c \'CREATE SCHEMA IF NOT EXISTS key;\'"]
envFrom:
- configMapRef:
name: postgres-config
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: data
dnsPolicy: ClusterFirst
restartPolicy: Never
volumes:
- name: data
emptyDir: {}
status: {}
Upvotes: 5
Reputation: 11940
The new DB schema can be create on the running Pod:
kubectl get po
kubectl exec -it [Name-of-Pod] -- sh
kubectl cp [Name-of-File] [Name-of-Pod]:[Name-of-File]
psql -U username dbname < [Name-of-File]
Upvotes: 0