jackc/pgx · error

not a string and cannot be encoded as text

Error message

not a string and cannot be encoded as text

What it means

tryScanStringCopyValueThenEncode guard: a COPY value was not a Go string and also could not be text-encoded by the type map (the inner m.Encode failed), so no text representation could be produced for the copy path. The unsupported argument value is the faulty input; the error deliberately masks the inner encode error.

Solutions

  1. Pass values of types the type map can encode for the column's OID
  2. Register an encoder for the custom type being copied
  3. Convert the value to a supported Go type before CopyFrom
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at values.go:51 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/8e6b30fba2f86e48. Report an issue: GitHub.

Appendix: source

Thrown at values.go:51

			argBuf = argBuf2
		} else {
			return nil, err
		}
	}

	if argBuf != nil {
		buf = argBuf
		pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
	}
	return buf, nil
}

func tryScanStringCopyValueThenEncode(m *pgtype.Map, buf []byte, oid uint32, arg any) ([]byte, error) {
	s, ok := arg.(string)
	if !ok {
		textBuf, err := m.Encode(oid, TextFormatCode, arg, nil)
		if err != nil {
			return nil, errors.New("not a string and cannot be encoded as text")
		}
		s = string(textBuf)
	}

	var v any
	err := m.Scan(oid, TextFormatCode, []byte(s), &v)
	if err != nil {
		return nil, err
	}

	return m.Encode(oid, BinaryFormatCode, v, buf)
}

View on GitHub (pinned to ec1a0befd2)