jackc/pgx · error
unterminated quoted string in connection info string
Error message
unterminated quoted string in connection info string
What it means
Returned by parseKeywordValueSettings while parsing a single-quoted value: the closing quote is never found before end-of-string. The parser scans until it runs out of bytes without encountering the terminating ', so the quoted value is unterminated.
Source
Thrown at pgconn/config.go:763
}
}
val = strings.ReplaceAll(strings.ReplaceAll(s[:end], "\\\\", "\\"), "\\'", "'")
// Consume the value and trim any subsequent whitespace so that
// multiple trailing spaces don't cause a spurious parse failure.
s = strings.TrimLeft(s[end:], " \t\n\r\v\f")
default: // quoted string
s = s[1:]
end := 0
for ; end < len(s); end++ {
if s[end] == '\'' {
break
}
if s[end] == '\\' {
end++
}
}
if end == len(s) {
return nil, errors.New("unterminated quoted string in connection info string")
}
val = strings.ReplaceAll(strings.ReplaceAll(s[:end], "\\\\", "\\"), "\\'", "'")
// Consume the closing quote and any subsequent whitespace.
s = strings.TrimLeft(s[end+1:], " \t\n\r\v\f")
}
key = canonicalConnStringKey(key)
if key == "" {
return nil, errors.New("invalid keyword/value")
}
if key == "user" && val == "" {
continue
}
settings[key] = val
}
View on GitHub (pinned to ec1a0befd2)
Solutions
- Add the missing closing single-quote: "password='secret'".
- To include a literal quote inside a quoted value, escape it as \' or double it per libpq rules.
- Prefer passing a URL connection string or building a *ConnConfig to avoid manual quoting.
Example fix
// before pgx.ParseConfig(`user=app password='secret`) // after pgx.ParseConfig(`user=app password='secret'`)
Defensive patterns
Strategy: validation
Validate before calling
// Ensure every opened single-quote in conninfo is closed.
func validateQuotesBalanced(s string) error {
inQuote := false
for i := 0; i < len(s); i++ {
if s[i] == '\'' {
inQuote = !inQuote
}
}
if inQuote {
return errors.New("unterminated quoted string in connection info string")
}
return nil
} Try / catch
if _, err := pgx.ParseConfig(connStr); err != nil {
if strings.Contains(err.Error(), "unterminated quoted string") {
return fmt.Errorf("connection string has an unclosed quote: %w", err)
}
} Prevention
- Always close single-quoted values; escape literal quotes as \\'.
- For values with spaces/special chars, prefer URL DSN encoding or *ConnConfig.
- Lint dynamically built conninfo before use.
When it happens
Trigger: A keyword/value string with an unclosed single-quoted value, e.g. "password='secret" or "dbname='my db". Common when a value contains spaces and the opening quote is added but the closing one is dropped.
Common situations: Building conninfo dynamically and forgetting to close a quote; values with spaces/apostrophes mishandled; shell quoting eating the closing quote.
Related errors
- invalid keyword/value
- invalid backslash
- OAuth authentication required but no token provider configur
- unable to add CA to cert pool
AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04).
Data as JSON: /data/errors/21cd5d3b4cea285b.json.
Report an issue: GitHub.