Skip to content

Instantly share code, notes, and snippets.

@maxisme
Created February 22, 2021 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxisme/02b18b77f9919955eafa8c5792273279 to your computer and use it in GitHub Desktop.
Save maxisme/02b18b77f9919955eafa8c5792273279 to your computer and use it in GitHub Desktop.
#!/bin/bash
POSTGRES_PASSWORD=$(kubectl get secret --namespace default pg-postgresql-ha-postgresql -o jsonpath="{.data.postgresql-password}" | base64 --decode)
db_cmd="kubectl run pg-postgresql-ha-client --rm --tty -i --restart='Never' --overrides='{\"spec\": {\"nodeSelector\": {\"kubernetes.io/arch\": \"amd64\"}}}' --image docker.io/bitnami/postgresql-repmgr:11.10.0-debian-10-r55 --env='PGPASSWORD=$POSTGRES_PASSWORD' --command -- psql -h pg-postgresql-ha-pgpool -p 5432 -U postgres -d postgres"
read -p "Database name: " DB_NAME
read -p "Username for $DB_NAME: " USERNAME
read -p "Password for $USERNAME: " PASSWORD
eval "$db_cmd -c 'CREATE DATABASE $DB_NAME;'"
eval "$db_cmd -c \"CREATE USER $USERNAME WITH ENCRYPTED PASSWORD '$PASSWORD';\""
eval "$db_cmd -c \"GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $USERNAME;\""
eval "$db_cmd -c \"REVOKE connect ON DATABASE $DB_NAME FROM PUBLIC;\""
eval "$db_cmd -c \"GRANT connect ON DATABASE $DB_NAME TO $USERNAME;\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment