Database drivers, message brokers and many APIs take a URL that includes credentials: scheme://user:password@host/path. When the password contains a character that also has a job in URL syntax, the parser cuts the URL in the wrong place, and the error you see is usually a confusing authentication or host-not-found failure.
Why special characters break the URL
The credentials end at the last @ before the host, and : separates the user from the password. A password like p@ss:w0rd/#?$ contains an @, a :, a /, a # and a ?. Each one can end the credentials, start the path, the query or the fragment early.
Encoded, the same password is unambiguous:
postgresql://app_user:p%40ss%3Aw0rd%2F%23%3F%24@db.example.com:5432/appWhat the drivers say
- MongoDB documents that if the username or password includes
$ : / ? # [ ] @, those characters must be converted using percent-encoding. Its own example writes a password containing@asD1fficultP%40ssw0rd(MongoDB connection string formats). - PostgreSQL (libpq) says a connection URI needs percent-encoding if it includes symbols with special meaning in any of its parts (libpq connection strings).
- For any other driver, check its documentation; the RFC 3986 rule below is always safe.
How to encode it, by language
Outputs below were produced by running each function, for the password p@ss:w0rd/#?$.
| Call | Output | Runtime |
|---|---|---|
| encodeURIComponent(password) | p%40ss%3Aw0rd%2F%23%3F%24 | Node.js 18.20.7 (V8 10.2.154.26-node.39) |
| urllib.parse.quote(password, safe='') | p%40ss%3Aw0rd%2F%23%3F%24 | Python 3.12.6 |
| urllib.parse.quote(password) ← keeps / | p%40ss%3Aw0rd/%23%3F%24 | Python 3.12.6 |
| Uri.EscapeDataString(password) | p%40ss%3Aw0rd%2F%23%3F%24 | .NET 8.0.6 |
Note the third row: Python's quote keeps / by default, which leaves the password broken. Always pass safe=''. Form-style encoders that write spaces as +, such as quote_plus or URLSearchParams, are also the wrong choice for credentials: a URL parser does not turn + back into a space. Compare every function.
Checklist
- Encode the username and the password separately, never the whole URL.
- Encode once. If the password already contains
%sequences from an earlier step, do not encode it again. - Check the result in the URL Parser: the username and password should appear in their own fields and the host should be correct.
- Keep the URL out of logs and source control: encoding hides nothing.