Google Cloud SQL Proxy Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
get_instances() { | |
local IFS=$'\n' | |
instances=($(gcloud sql instances list --format="table(name,database_version,region,STATUS, connectionName)")) | |
} | |
get_instances | |
for i in "${!instances[@]}"; do | |
[[ $i = 0 ]] && output="INDEX" || output="$i" | |
printf "%-5s\t%s\n" "$output" "${instances[$i]}"; | |
done | |
read -p "Instance Index: " instance_index | |
if [[ ${instance_index} =~ ^[0-9]+$ ]] && \ | |
[ ${instance_index} -ge 1 ] && [ ${instance_index} -le ${#instances[@]} ]; then | |
instance_line=${instances[${instance_index}]} | |
connection_name=$(awk '{print $5}' <<< ${instance_line}) | |
db_type=$(awk '{print $2}' <<< ${instance_line}) | |
if [[ ${db_type} =~ ^POSTGRES ]]; then | |
port=${1:-5432} | |
elif [[ ${db_type} =~ ^MYSQL ]]; then | |
port=${1:-3306} | |
else | |
echo "Unknown DB Type ${db_type}" && exit 1; | |
fi | |
${CLOUD_PROXY_SQL_BIN:-cloud_sql_proxy} -instances=${connection_name}=tcp:${port} | |
else | |
echo "Invalid Instance Index." >&2 && exit 1; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This helper to interactive select which database instance to proxy over TCP expects
cloud_sql_proxy
to either be on thePATH
or specified in an environment variableCLOUD_PROXY_SQL_BIN
.Checkout the Cloud SQL Proxy page. for more information.