|
| 1 | +# Cloud SQL Auth Proxy |
| 2 | + |
| 3 | +### Set instance connection name |
| 4 | + |
| 5 | +```bash |
| 6 | +export INSTANCE_CONNECTION_NAME="lofty-root-378503:us-central1:jm-pg-demo" |
| 7 | +``` |
| 8 | + |
| 9 | +### Run the CloudSQL auth proxy |
| 10 | + |
| 11 | +Remove any old proxy container: |
| 12 | + |
| 13 | +```bash |
| 14 | +docker rm -f csql-proxy 2>/dev/null || true |
| 15 | +``` |
| 16 | + |
| 17 | +Then use one of the below options: |
| 18 | + |
| 19 | +#### Executable |
| 20 | + |
| 21 | +```bash |
| 22 | +# CLI option |
| 23 | +# Download from: https://cloud.google.com/sql/docs/postgres/connect-auth-proxy |
| 24 | +./cloud-sql-proxy ${INSTANCE_CONNECTION_NAME} --port 5432 & |
| 25 | +PROXY_PID=$! |
| 26 | +``` |
| 27 | + |
| 28 | +#### Docker |
| 29 | + |
| 30 | +```bash |
| 31 | + |
| 32 | +export CREDENTIALS_PATH="/Users/admin/.config/gcloud/sa-private-key.json" |
| 33 | +docker run -d --name csql-proxy -p 127.0.0.1:5400:5432 \ |
| 34 | + -v "$GOOGLE_APPLICATION_CREDENTIALS:/creds/key.json:ro" \ |
| 35 | + gcr.io/cloud-sql-connectors/cloud-sql-proxy:2 \ |
| 36 | + --address 0.0.0.0 --port 5432 \ |
| 37 | + --credentials-file=/creds/key.json "$INSTANCE_CONNECTION_NAME" |
| 38 | +``` |
| 39 | + |
| 40 | +### Wait for proxy to listen |
| 41 | + |
| 42 | +```bash |
| 43 | +for i in {1..30}; do nc -z 127.0.0.1 5433 && echo "proxy ready" && break; sleep 1; done |
| 44 | +``` |
| 45 | + |
| 46 | +### Check if proxy is running |
| 47 | + |
| 48 | +```bash |
| 49 | +docker ps -a | grep csql-proxy || true |
| 50 | +``` |
| 51 | + |
| 52 | +### Verify proxy is running on a specific port |
| 53 | + |
| 54 | +```bash |
| 55 | +nc -z 127.0.0.1 5400 && echo "proxy up" || echo "proxy not up" |
| 56 | +``` |
| 57 | + |
| 58 | +### Check proxy logs |
| 59 | + |
| 60 | +```bash |
| 61 | +docker logs csql-proxy --tail=200 || true |
| 62 | +``` |
| 63 | + |
| 64 | +### Check if port is in use |
| 65 | + |
| 66 | +```bash |
| 67 | +lsof -iTCP:5432 -sTCP:LISTEN || true |
| 68 | +``` |
| 69 | + |
| 70 | +### List available ports |
| 71 | + |
| 72 | +#### Common Postgres ports |
| 73 | + |
| 74 | +```bash |
| 75 | +for p in 5432 5433 5434 5435 5436; do (lsof -iTCP:$p -sTCP:LISTEN >/dev/null) && echo "❌ $p in use" || echo "✅ $p free"; done |
| 76 | +``` |
| 77 | + |
| 78 | +#### Scan a wider port range |
| 79 | + |
| 80 | +```bash |
| 81 | +for p in {5400..5450}; do (lsof -iTCP:$p -sTCP:LISTEN >/dev/null) || echo "✅ $p free"; done |
| 82 | +``` |
| 83 | + |
| 84 | +#### Pick the first available port |
| 85 | + |
| 86 | +```bash |
| 87 | +for p in {5400..5450}; do (lsof -iTCP:$p -sTCP:LISTEN >/dev/null) || { echo "First free port: $p"; break; }; done |
| 88 | +``` |
| 89 | + |
| 90 | +### Test connection |
| 91 | + |
| 92 | +```bash |
| 93 | +export TF_VAR_db_user=username |
| 94 | +export TF_VAR_db_password=password |
| 95 | +export TF_VAR_db_name=jm_demo_db |
| 96 | +``` |
| 97 | + |
| 98 | +Have the proxy running and healthy to `127.0.0.1:5432`. |
| 99 | +Make sure nothing else is using port `5432`. |
| 100 | +If it is, pick another port (e.g., 5433) and update your `psql` command. |
| 101 | + |
| 102 | +Then run: |
| 103 | + |
| 104 | +```bash |
| 105 | +PGPASSWORD="$TF_VAR_db_password" psql \ |
| 106 | + -h 127.0.0.1 -p 5400 \ |
| 107 | + -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" \ |
| 108 | + -c "select 1;" |
| 109 | +``` |
| 110 | + |
| 111 | +### If the test is successful |
| 112 | + |
| 113 | +Assuming the file `/src/test/resources/init.sql` exists: |
| 114 | + |
| 115 | +```bash |
| 116 | +PGPASSWORD="$TF_VAR_db_password" psql \ |
| 117 | + -h 127.0.0.1 -p 5400 \ |
| 118 | + -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" \ |
| 119 | + -f init.sql |
| 120 | +``` |
| 121 | + |
| 122 | +You can also create the table directly from the command line: |
| 123 | + |
| 124 | +```bash |
| 125 | +cat > init.sql <<'SQL' |
| 126 | +PGPASSWORD="$TF_VAR_db_password" psql -h 127.0.0.1 -p 5400 -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" -v ON_ERROR_STOP=1 <<'SQL' |
| 127 | +CREATE TABLE IF NOT EXISTS widgets ( |
| 128 | + id UUID PRIMARY KEY, |
| 129 | + name TEXT NOT NULL, |
| 130 | + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), |
| 131 | + meta JSONB |
| 132 | +); |
| 133 | +SQL |
| 134 | +``` |
| 135 | + |
| 136 | +### Single insert |
| 137 | + |
| 138 | +```bash |
| 139 | +PGPASSWORD="$TF_VAR_db_password" psql \ |
| 140 | + -h 127.0.0.1 -p 5400 \ |
| 141 | + -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" \ |
| 142 | + -c "INSERT INTO widgets (id, name, meta) VALUES (gen_random_uuid(), 'Test Widget', '{\"color\":\"red\",\"size\":10}');" |
| 143 | +``` |
| 144 | + |
| 145 | +### Multiple inserts |
| 146 | + |
| 147 | +```bash |
| 148 | +PGPASSWORD="$TF_VAR_db_password" psql \ |
| 149 | + -h 127.0.0.1 -p 5400 \ |
| 150 | + -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" \ |
| 151 | + -c " |
| 152 | +INSERT INTO widgets (id, name, meta) VALUES |
| 153 | + (gen_random_uuid(), 'Widget A', '{\"type\":\"alpha\"}'), |
| 154 | + (gen_random_uuid(), 'Widget B', '{\"type\":\"beta\"}'), |
| 155 | + (gen_random_uuid(), 'Widget C', '{\"type\":\"gamma\"}'); |
| 156 | +" |
| 157 | +``` |
| 158 | + |
| 159 | +### Insert rows from a file |
| 160 | + |
| 161 | +```bash |
| 162 | +cat > /tmp/insert_widgets.sql <<'SQL' |
| 163 | +INSERT INTO widgets (id, name, meta) VALUES |
| 164 | + (gen_random_uuid(), 'Widget X', '{"size":42}'), |
| 165 | + (gen_random_uuid(), 'Widget Y', '{"color":"blue"}'); |
| 166 | +SQL |
| 167 | + |
| 168 | +PGPASSWORD="$TF_VAR_db_password" psql \ |
| 169 | + -h 127.0.0.1 -p 5400 \ |
| 170 | + -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" \ |
| 171 | + -f insert_widgets.sql |
| 172 | +``` |
| 173 | + |
| 174 | +### Read all rows in a table |
| 175 | + |
| 176 | +```bash |
| 177 | +PGPASSWORD="$TF_VAR_db_password" psql \ |
| 178 | + -h 127.0.0.1 -p 5400 \ |
| 179 | + -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" \ |
| 180 | + -c "SELECT * FROM widgets;" |
| 181 | +``` |
| 182 | + |
| 183 | +### Open an interactive psql session |
| 184 | + |
| 185 | +```bash |
| 186 | +PGPASSWORD="$TF_VAR_db_password" psql -h 127.0.0.1 -p 5400 -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" |
| 187 | +``` |
| 188 | + |
| 189 | +```sql |
| 190 | +INSERT INTO widgets (id, name, meta) VALUES (gen_random_uuid(), 'Widget ABC', '{"shape":"sphere"}'); |
| 191 | +SELECT * FROM widgets; |
| 192 | +``` |
| 193 | + |
| 194 | +### List tables |
| 195 | + |
| 196 | +```psql |
| 197 | +\l |
| 198 | +``` |
| 199 | + |
| 200 | +### Drop tables |
| 201 | + |
| 202 | +```bash |
| 203 | +DROP TABLE widgets; |
| 204 | +``` |
| 205 | + |
| 206 | +### Exit `psql` |
| 207 | + |
| 208 | +```psql |
| 209 | +exit |
| 210 | +``` |
| 211 | + |
| 212 | +--- |
| 213 | + |
| 214 | +### Grant create/use to a user |
| 215 | + |
| 216 | +```bash |
| 217 | +PGPASSWORD="$TF_VAR_db_password" psql -h 127.0.0.1 -p 5432 -U "$TF_VAR_db_user" -d "$TF_VAR_db_name" -c \ |
| 218 | +"GRANT USAGE ON SCHEMA public TO $TF_VAR_db_user; GRANT CREATE ON SCHEMA public TO $TF_VAR_db_user;" |
| 219 | +``` |
| 220 | + |
| 221 | +Verify `roles/cloudsql.client` on the user's service account. |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +### Stop the CloudSQL auth proxy |
| 226 | + |
| 227 | +```bash |
| 228 | +kill $PROXY_PID 2>/dev/null || true |
| 229 | +# or: docker rm -f csql-proxy |
| 230 | +``` |
0 commit comments