The `postgres-bytea` library provides robust functionality for parsing and encoding PostgreSQL `bytea` binary strings within Node.js applications. It supports both the modern 'hex' format (prefixed with `\x`) used in PostgreSQL 9.0 and later, as well as the older 'escape' format from PostgreSQL 8 and earlier, automatically detecting the input format. The current stable version is 3.0.0. While specific release cadence is not explicitly stated, the package appears actively maintained given recent NPM activity. Its key differentiation lies in its dual approach to `bytea` handling: a direct `decode` function for quick conversions and stream-based `Decoder` and `Encoder` classes for handling larger data volumes efficiently, particularly useful with PostgreSQL's `COPY` commands.
npm install postgres-byteaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to decode a PostgreSQL `bytea` hex string into a Node.js Buffer and how to encode a Buffer back into a `bytea` string using the stream API. It highlights the expected string formats for direct `decode` and stream-based `Encoder`/`Decoder` operations.
Review the official changelog or migration guide on the GitHub repository when upgrading from previous major versions. Test your application thoroughly after upgrade.
Ensure the input `bytea` string format matches the expected format for the specific API (single `\x` for `decode`, double `\\x` for streams). If using `COPY` commands, be mindful of the `bytea_output` setting in PostgreSQL and adjust accordingly.
For new applications, configure PostgreSQL to use `bytea_output = 'hex'` (which is the default in modern Postgres versions). If interacting with older systems or applications expecting the escape format, ensure your database connection or queries explicitly handle the output format.
Verify that the `bytea` string retrieved from PostgreSQL includes the correct prefix and only contains valid hexadecimal characters (0-9, a-f, A-F) after the prefix for hex format. Ensure backslashes are correctly escaped if manually constructing the string, especially when dealing with stream APIs that expect double backslashes.
If using ES Modules, prefer `import { decode } from 'postgres-bytea';`. If using CommonJS, use `const { decode } = require('postgres-bytea');` or `const bytea = require('postgres-bytea'); const decode = bytea;` if `decode` is the default export (which it is for compatibility). Check the `package.json` `type` field if present, or test import behavior.No dependency data recorded yet.