URL encoder/decoder · Guide
URL Encode Password: Credentials in Connection Strings
Connection strings such as `postgres://user:pass@host/db` are URLs, so a password containing `@`, `:`, `/` or `#` breaks the parse. Encoding it fixes the parse — and raises a separate question about whether the password should be in a URL at all.
Which characters break it
@ separates credentials from host, so a password containing it makes the parser see the wrong hostname. : separates user from password, / starts the path, # starts a fragment and silently truncates everything after it.
A password like p@ss:w0rd/! must be written p%40ss%3Aw0rd%2F%21. The # case is the nastiest — it does not error, it just quietly drops the rest of the string, giving an authentication failure with no clue why.
Encoding it correctly
Percent-encode the password component only, never the whole connection string:
# Python
from urllib.parse import quote
dsn = f"postgres://user:{quote(password, safe='')}@host:5432/db"
// Node
const dsn = `postgres://user:${encodeURIComponent(password)}@host:5432/db`;safe="" matters in Python — the default leaves / unencoded, which is exactly one of the characters that breaks the URL.
Better: keep it out of the URL
A password inside a connection string ends up in process listings, shell history, crash dumps, error messages and logs. Encoding does nothing about any of that.
Most drivers accept credentials as separate parameters rather than a URL — use that form when available. Otherwise keep the DSN in an environment variable or a secret manager, never in source control, and make sure your error handler redacts it before logging.
If a password must go through a URL, prefer generating one from an alphanumeric alphabet. It sidesteps the encoding question entirely, and length matters far more than symbol variety for strength.
Frequently asked questions
Which characters must I encode in a password?
At minimum @ : / ? # [ ] and %. Encoding everything non-alphanumeric is simplest and always safe.
My connection fails after the # in my password — why?
An unencoded # starts a URL fragment, so everything after it is dropped. Encode it as %23.
Is an encoded password in a URL secure?
No. Encoding is about parsing, not secrecy. The value still appears in logs and process lists.
Ready to try it?
Open the free browser-based URL encoder/decoder and apply what you just read — no sign-up, runs locally.
Open the URL encoder/decoder tool