Skip to main content

bit

Learning Focus

Use this lesson to understand PostgreSQL bit(n) and bit varying(n) and when they make sense.

bit Overview

  • bit(n): fixed-length bit string
  • bit varying(n): variable-length bit string

Typical use cases:

  • compact bit flags when you truly need bitwise operations
  • protocol/telemetry fields

Example

CREATE TABLE feature_masks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
flags bit(8) NOT NULL
);

Best Practices

  • Prefer boolean columns for readability when flags are independent.
  • Use bit only when bitwise operations or fixed masks are required.

What's Next