jackc/pgx · error

invalid keyword/value

Error message

invalid keyword/value

What it means

Returned by parseKeywordValueSettings when a segment of a keyword/value connection string contains no '=' character. libpq-style conninfo requires key=value pairs separated by whitespace; a missing '=' means the string is not parseable as a pair.

Source

Thrown at pgconn/config.go:727

}

func isIPOnly(host string) bool {
	return net.ParseIP(strings.Trim(host, "[]")) != nil || !strings.Contains(host, ":")
}

var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}

func parseKeywordValueSettings(s string) (map[string]string, error) {
	settings := make(map[string]string)

	// Trim any leading whitespace so that the loop exits cleanly when only
	// spaces remain (e.g. trailing spaces after the last value).
	s = strings.TrimLeft(s, " \t\n\r\v\f")
	for len(s) > 0 {
		var key, val string
		eqIdx := strings.IndexRune(s, '=')
		if eqIdx < 0 {
			return nil, errors.New("invalid keyword/value")
		}

		key = strings.Trim(s[:eqIdx], " \t\n\r\v\f")
		s = strings.TrimLeft(s[eqIdx+1:], " \t\n\r\v\f")
		switch {
		case len(s) == 0:
		case s[0] != '\'':
			end := 0
			for ; end < len(s); end++ {
				if asciiSpace[s[end]] == 1 {
					break
				}
				if s[end] == '\\' {
					end++
					if end == len(s) {
						return nil, errors.New("invalid backslash")
					}
				}

View on GitHub (pinned to ec1a0befd2)

Solutions

  1. Rewrite the connection string as key=value pairs: 'host=localhost user=app dbname=db'.
  2. If you meant to use a URL, prefix with 'postgres://': 'postgres://app@localhost/db'.
  3. Validate the conninfo string format before passing it to ParseConfig.

Example fix

// before
cfg, err := pgx.ParseConfig("host localhost user app")

// after
cfg, err := pgx.ParseConfig("host=localhost user=app")
Defensive patterns

Strategy: validation

Validate before calling

// Cheap preflight: every non-quoted token pair must contain '='.
func validateKeywordValue(s string) error {
    s = strings.TrimSpace(s)
    for len(s) > 0 {
        eq := strings.IndexRune(s, '=')
        if eq < 0 {
            return fmt.Errorf("invalid keyword/value near %q", s)
        }
        // advance past this pair (naive; full parser is in pgx)
        s = strings.TrimSpace(s[eq+1:])
    }
    return nil
}

Try / catch

if _, err := pgx.ParseConfig(connStr); err != nil {
    if strings.Contains(err.Error(), "invalid keyword/value") {
        return fmt.Errorf("connection string is not valid key=value form: %w", err)
    }
}

Prevention

When it happens

Trigger: Calling pgx.ParseConfig (or Connect) with a keyword/value string like 'host localhost' (missing '=') or a stray token. The parser finds no '=' before the next key boundary.

Common situations: Typos in conninfo strings; copy-pasting a URL-style host without the scheme; trailing tokens after a value; mixing URL and keyword syntax.

Related errors


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /data/errors/323bf9788c1b67f3.json. Report an issue: GitHub.