Kubernetes
CloudSQL
password escaping
special characters
troubleshooting

Problem with escaping password with special characters in Kubernetes cloudsql

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Special characters in a database password are usually not a Kubernetes problem by themselves. The trouble starts when the same password passes through YAML, base64, shells, connection strings, and application configuration. Each layer has its own parsing rules, so a password that is valid in Cloud SQL can still break once it is embedded incorrectly in a secret or URL.

Store the Raw Password in the Secret

The safest approach is to avoid manual escaping whenever possible and store the raw value in a Kubernetes secret. Using stringData is usually easier than building base64 values yourself because Kubernetes will encode the data for you.

yaml
1apiVersion: v1
2kind: Secret
3metadata:
4  name: db-credentials
5stringData:
6  username: app_user
7  password: 'p@ss:word$with#chars!'

The quotes in YAML protect characters that YAML itself might otherwise parse in a surprising way. Once the secret is created, Kubernetes stores the bytes exactly as given.

Inject the Secret as an Environment Variable

If the application reads the password from an environment variable, keep the password separate from the connection string:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: api
5spec:
6  replicas: 1
7  selector:
8    matchLabels:
9      app: api
10  template:
11    metadata:
12      labels:
13        app: api
14    spec:
15      containers:
16        - name: api
17          image: example/api:latest
18          env:
19            - name: DB_PASSWORD
20              valueFrom:
21                secretKeyRef:
22                  name: db-credentials
23                  key: password

This avoids shell escaping and keeps the password out of the manifest body after the secret is defined.

The Real Escape Problem Is Often the URL

Many connection failures happen because the password is inserted directly into a database URL. In a URL, characters such as @, :, /, ?, and # have special meaning. If the password contains any of them, it must be URL-encoded before building the DSN.

For example, build the URL safely in Python like this:

python
1from urllib.parse import quote_plus
2import os
3
4username = os.environ["DB_USER"]
5password = quote_plus(os.environ["DB_PASSWORD"])
6host = os.environ["DB_HOST"]
7database = os.environ["DB_NAME"]
8
9url = f"postgresql://{username}:{password}@{host}/{database}"
10print(url)

The important point is that Kubernetes should store the raw password, while the application encodes it only when it inserts the value into a URL.

Shell Commands Add Another Layer

If you create secrets from the command line, quote the value so the shell does not interpret characters before kubectl even receives them:

bash
kubectl create secret generic db-credentials \
  --from-literal=username='app_user' \
  --from-literal=password='p@ss:word$with#chars!'

Without the quotes, characters such as $ may be expanded by the shell into environment-variable references, which silently changes the password.

Cloud SQL Does Not Need a Different Password Format

Cloud SQL itself does not require you to weaken the password just because Kubernetes is involved. The correct fix is usually to preserve the original bytes through each configuration layer rather than stripping special characters out of the password.

If the connection still fails, test the raw secret value inside the container, then verify whether the application is reading it directly or embedding it into a URI without encoding.

Common Pitfalls

The most common mistake is base64-encoding the password incorrectly by hand and then blaming special characters. stringData avoids that whole class of error.

Another issue is assuming the password must be escaped the same way in every context. YAML quoting, shell quoting, and URL encoding are different operations.

Developers also put the password directly into a connection string and forget that URL reserved characters must be encoded.

Summary

  • Store the raw password in a Kubernetes secret, ideally with stringData.
  • Quote the value in YAML and shell commands so intermediary parsers do not alter it.
  • Keep the password separate from the DSN until the application builds the final connection string.
  • URL-encode the password only when inserting it into a database URL.
  • Most failures come from the configuration layer around the password, not from Cloud SQL itself.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.