#!/bin/sh choosefrom () { select option; do # in "$@" is the default if [ 1 -le "$REPLY" ] && [ "$REPLY" -le $(($#)) ]; then echo $option break; fi done } echo "connecting to cluster" default_pg_cluster_name=${pg_cluster_name:=$(kubectl get postgresql -ojsonpath="{.items[0].metadata.name}")} echo -e "\npostgres cluster name?" pg_cluster_name=$(choosefrom ${pg_cluster_name:=$(kubectl get services -ojsonpath="{.items[*].metadata.name}")}) echo -e "\nuser?" pg_user_name=$(choosefrom $(kubectl get secrets -o json | jq -r '.items[] | .data.username | select(. != null) | (@base64d)')) echo -e "\ndatabase? default=postgres" read pg_database pg_database=${pg_database:="postgres"} echo -e "\nhost port? default=5444" read pg_host_port pg_host_port=${pg_host_port:="5444"} PGMASTER=$(kubectl get pods -o jsonpath={.items..metadata.name} -l application=spilo,cluster-name=$pg_cluster_name,spilo-role=master -n default) echo "about to forward $pg_cluster_name $PGMASTER to $pg_host_port" kubectl port-forward $PGMASTER "$pg_host_port:5432" & export PGPASSWORD=$(kubectl get secret $(echo $pg_user_name | sed -e "s/_/-/g").$pg_cluster_name.credentials.postgresql.acid.zalan.do -o 'jsonpath={.data.password}' | base64 -d) while true do psql postgres://$pg_user_name:$PGPASSWORD@localhost:$pg_host_port/$pg_database sleep 1 done