Invalid URL / getaddrinfo ENOTFOUND on your connection string: your generated password contains a character the URL cannot carry
A password containing /, #, ?, @ or % breaks a connection string, and the error depends on the character and on the library. The mechanism, the rule, the check.
getaddrinfo ENOTFOUNDIn short
- Symptom
- One cause, several messages depending on the character and the library:
TypeError: Invalid URL, agetaddrinfo ENOTFOUNDon a host named after the user, a connection refused on the client's default host, or authentication refused while the password is correct. - Real cause
- A connection string is a URL. The characters
/,#and?end its authority,@separates the password from the host, and%announces an encoded character. Strong password generators produce those characters. - Fix
- Generate secrets bound to a connection string in an alphabet with no structural character (
openssl rand -hex 32), or better, do not build a string at all and pass the fields separately. - Prevention
- Refuse to boot if a DSN-bound secret contains a character outside the alphabet, with the replacement command in the error message.
- Updated
- 2026-10-14
- Read time
- 11 min
- Access
- open
The answer, in three lines
A connection string, or DSN, is the text that gives an application the host, the user and the
password of its database, written like a web address. It is a URL, and a password placed inside it
follows URL rules. The characters /, # and ? end the part that carries the host, @
separates the password from the host, and % announces an encoded character.
Strong password generators produce those characters. The fix: openssl rand -hex 32 for any
secret destined for a connection string. And wherever possible, do not build a string at all.
The symptom
One cause, and a different message depending on the character and on the library. It took three incidents before a rule came out of it.
The original incidents kept only three traces of a message: ECONNREFUSED errors on a Redis cache,
a Go client that rejected the string with invalid port ":…" after host, and a TypeError thrown
by a Node client, whose text was not kept. The last section says which incident left which trace.
All the output below is reproduced, on 14 October 2026, with fabricated strings, under Node 22.23.3 and against PostgreSQL 16 and Redis 7.
Symptom A, the parse error. The application refuses to start. This is what current versions of
the clients give (pg 8.23.1, postgres 3.4.9, ioredis 5.11.1, redis 5.12.1) when the password
contains a /, a # or a ? after letters:
$ node -e "new URL('redis://default:ab/cd+ef@cache-interne:6379/0')"
TypeError: Invalid URL
at new URL (node:internal/url:818:25)
Symptom B, the crooked host. The string parses, but the host becomes the user name. ioredis up
to 5.10.1 and pg-connection-string 2.5.0, which still read the string with Node's old url.parse,
give it on that kind of string. Current clients give it too, depending on how the password starts
(see the table further down):
Error: getaddrinfo ENOTFOUND default
With psql, it is an @ that sends you to a host that does not exist, because psql cuts the
string at the first @:
psql: error: could not translate host name "w0rd@db-interne" to address: Name does not resolve
A variant, when the user is empty (redis://:password@host): with ioredis up to 5.10.1, the host
disappears, and the client quietly falls back to its default host. The first incident produced
ECONNREFUSED errors, and reproducing it with the string and the client version of the time gives
this:
Error: connect ECONNREFUSED 127.0.0.1:6379
Symptom C, authentication refused. Everything starts, the host is right, and the server rejects
the password. A % followed by two hexadecimal digits in the password is enough: x%41y is
decoded into xAy, and that is the password the server receives. Reproduced with pg, postgres
and psql against PostgreSQL 16:
error: password authentication failed for user "app"
On the Redis side, the same password gives WRONGPASS invalid username-password pair or user is disabled.
And yet the password is exactly the one in the secret manager. You copy it, paste it into a
command-line client with the -W option: it works. Through the connection string: it does not.
The wrong diagnosis
The incident record does not say which wrong leads were followed. These are the two usual ones in front of such messages.
The first: "the secret is not mounted properly, a stray space or newline is hanging around". It is
the most frequent secret failure: an echo instead of a printf, a newline slipped into a
base64-encoded secret. So you check how the secret travels: you print it as hex, you count the
bytes, you compare it character by character. And the secret is correct. You checked the one thing
you suspected, it is fine, and the investigation stalls.
The second, when symptom C appears inside a Kubernetes cluster: "it's the network, or a security rule". It is all the more tempting because PostgreSQL returns the same message when the connection lands on the wrong server.
In both cases, you reason about the password, and nobody asks what happens to it once it is placed inside a URL.
What is actually happening
In scheme://user:password@host:port/database, the URL parser of the WHATWG standard (the one
behind new URL) first reads the authority: everything after //, up to the first /, ? or #.
It carries the user, the password, the host and the port. These characters change the meaning of a
password:
| Character | What the parser does with it | Consequence |
|---|---|---|
/, # or ? | end of the authority: the host becomes the user name, and the start of the password becomes the port | depending on that start and on the library, symptom A or B (next table) |
@ | the parser cuts at the last @ and encodes the earlier ones as %40 | the host is right; the parsed password is pa$$%40w0rd instead of pa$$@w0rd |
% followed by two hexadecimal digits | an escape sequence, left as it is | the client decodes it and sends another password: symptom C |
With a /, a # or a ?, everything depends on what comes before it in the password, since that
piece becomes the port. Reproduced with fabricated PostgreSQL and Redis strings; <user> is the user
name in the string:
| Start of the password | pg 8.23.1, postgres 3.4.9, ioredis 5.11.0 and 5.11.1 | ioredis 4.31.0 to 5.10.1, pg-connection-string 2.5.0 | redis 4.7.1 and 5.12.1 |
|---|---|---|---|
letters (ab/…) | TypeError: Invalid URL | getaddrinfo ENOTFOUND <user> | TypeError: Invalid URL |
empty (/…) | getaddrinfo ENOTFOUND <user> | getaddrinfo ENOTFOUND <user> | TypeError: Invalid pathname |
digits, up to 65535 (12/…) | getaddrinfo ENOTFOUND <user> | getaddrinfo ENOTFOUND <user> | TypeError: Invalid pathname |
digits above 65535 (123456/…) | TypeError: Invalid URL | RangeError: Port should be >= 0 and < 65536 | TypeError: Invalid URL |
The ioredis boundary is in its code: up to 5.10.1 it reads the string with url.parse; from
5.11.0, released on 26 May 2026, with new URL. Versions tried: 4.31.0, 5.4.1, 5.8.2, 5.9.3,
5.10.1, 5.11.0 and 5.11.1.
Verifiable in three seconds (reproduced, Node 22.23.3):
$ node -e "console.log(new URL('postgres://app:pa\$\$@w0rd@db-interne:5432/app').password)"
pa$$%40w0rd
$ node -e "new URL('postgres://app:abc#def@db-interne:5432/app')"
TypeError: Invalid URL
In the first example the host is correct (db-interne), and the parsed password is pa$$%40w0rd.
pg and postgres decode it before sending it, and connect. The second one fails because abc
is not a port number: the # closed the authority right after abc.
So the same string can work with one library and fail with another, because two parsers do not
apply the same rules. psql reads the string through libpq, PostgreSQL's C library, which cuts at
the first @ and keeps # and ? inside the password. Reproduced against PostgreSQL 16:
| Password in the string | pg 8.23.1 and postgres 3.4.9 | psql 16.15 |
|---|---|---|
pa$$@w0rd | connects | could not translate host name "w0rd@db-interne" to address |
abc#def | TypeError: Invalid URL | connects |
And this is what makes it a recurring incident: strong password generators produce those
characters. openssl rand -base64 32, the generator behind the last of the three original
incidents, puts a / in roughly one password out of two (1 − (63/64)^42 ≈ 0.48). So it does not
break on every rotation, and the lesson is not learned the first time.
The fix
Step 1: Generate connection secrets in an alphabet with no structural character
Hexadecimal is the simplest:
openssl rand -hex 32 # 32 random bytes: 64 characters [0-9a-f], 256 bits of entropy
It is longer than a "strong" 16-character password, and stronger: 256 bits against about 105 for 16 printable ASCII characters. The length costs nothing: nobody types it.
Step 2: If you must keep an existing secret, encode it when you build the string
Never by hand.
const url = `postgres://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${host}:${port}/${db}`;
Step 3: Wherever possible, do not build a string at all
- const pool = new Pool({ connectionString: process.env.DATABASE_URL });
+ const pool = new Pool({
+ host: process.env.DB_HOST,
+ port: Number(process.env.DB_PORT),
+ user: process.env.DB_USER,
+ password: process.env.DB_PASSWORD, // no URL, no escaping, no trap
+ database: process.env.DB_NAME,
+ });
Both forms were tried with pg 8.23.1 on the password p/a#s?s@w%41rd, which gathers every
character in the table: both connect.
Check (pass or fail)
# will a Node client read DB_PASSWORD as the password?
node -e "
const url = process.env.DATABASE_URL;
const s = process.env.DB_PASSWORD;
if (url === undefined || s === undefined) { console.error('DATABASE_URL or DB_PASSWORD is missing: nothing to compare'); process.exit(2); }
let u;
try { u = new URL(url); } catch { console.error('DATABASE_URL does not parse as a URL'); process.exit(1); }
let lu;
try { lu = decodeURIComponent(u.password); } catch { console.error('the password in DATABASE_URL is badly encoded'); process.exit(1); }
if (lu !== s) { console.error('the password read from DATABASE_URL differs from DB_PASSWORD'); process.exit(1); }
console.log('host =', u.hostname, '| ok');
"
Exit 0: a Node client will read the right password. Exit 1: the failure. Exit 2: a variable is
missing. The script prints neither the secret nor the string: new URL copies the whole string
into its error, which is why that error is caught.
Tried under Node 22.23.3. It exits 0 for a hexadecimal password and for a password encoded with
encodeURIComponent, 1 for x%41y, x%zzy, ab/cd+ef, abc#def, abc?def and 12/cd, and 2
when DB_PASSWORD is missing. It lets an unencoded @ through: pg and postgres read it
correctly, psql does not. If psql also reads the string, encode the @.
Prevention
- Validation at boot. The application refuses to start if a DSN-bound secret contains a
character outside the alphabet, with a message that gives the replacement command: "Boot
refused: DB_PASSWORD contains a non-URL-safe character. Generate one with:
openssl rand -hex 32". Whoever reads the message does not have to look the command up elsewhere. - CI. The pass-or-fail check above, run against the integration environment.
- The team's secret generation script. That is the best control point: if the script emits hexadecimal, the rule no longer needs to be known.
- The repository instruction file. "DSN-bound secrets:
openssl rand -hex 32, never a generator with punctuation. Non-DSN-bound secrets: no alphabet constraint."
Why this matters with agents
An agent asked to "generate a strong password" will generate a strong password, with punctuation, because that is what "strong" means in the documentation it has read. It is right about the criterion it was given, and its password will break the connection string on one rotation out of two. The result is defensible and it is wrong: the textbook case of the under-specified instruction.
Across a fleet, frequency makes it worse. Provisioning ephemeral environments, rotating secrets, creating one database per agent branch: every generation is a draw. A developer who generates two secrets a quarter falls into the trap now and then, and forgets in between. A fleet that generates dozens a week falls into it every week.
The diagnosis also costs an agent more: symptom C ("authentication failed") sends it into permissions and networking, where it can search for a long time, methodically. The check that settles it is one line: compare the parsed password, once decoded, to the secret. You still have to know it exists.
What this entry does not cover
The case where the secret is correct but the tool handling it is compromised: that is a different class of problem, and it has its own entry.
Where this entry comes from
Three incidents between March and April 2026, recorded in the history of a personal infrastructure
configuration repository: two on Redis caches, one on a PostgreSQL database. All three passwords
contained +, / and =, the base64 alphabet; the last one came from openssl rand -base64 32.
The first two incidents were fixed by encoding the password inside the string, the third by
replacing it with a hexadecimal secret, and the rule was written after that one. What the record
says about each incident:
- the first cache: the fix's message mentions
ECONNREFUSEDerrors. The string had an empty user, and reproducing it with the client version of the time (ioredis5.9.3) givesconnect ECONNREFUSED 127.0.0.1:6379; - the second cache: the record only says that the password broke URL parsing. The message was not kept;
- the database: the note written after the third incident mentions a Go client that rejected the
string (
invalid port ":…" after host) and a Node client that threw aTypeError, without keeping its exact text.
None of the three produced symptom C or a getaddrinfo ENOTFOUND: those two messages come from the
reproductions. The transcripts of the diagnosis sessions were not kept.
All the terminal output in this entry was reproduced on 14 October 2026, in throwaway containers,
with the versions given next to each one: Node 22.23.3, PostgreSQL 16, Redis 7. The connection
strings are fabricated. The character table follows the WHATWG URL standard ("authority" and "port"
states); the PostgreSQL message is the one in its source code (src/backend/libpq/auth.c,
version 16). The ioredis version boundary comes from its published code (built/utils/index.js,
the parseURL function, in 5.10.1 and 5.11.0).
This entry is part of a notebook of 27 production failures hit between February and September 2026. Some are public and free: see the notebook.
The others are published as they are written, along with the modules and their exercises, as part of the paid product. In that product, the modules are in English; the exercises and the other entries are in French. Nothing on this page is a teaser for it.