Skip to content

pg_postal_code_ca

an efficient, validated, formatted Canadian postal code type

pg_postal_code_ca · an efficient, validated, formatted Canadian postal code type

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100263pg_postal_code_capg_postal_code_ca1.0TYPEPostgreSQLC

Usage

Sources:

pg_postal_code_ca provides a compact, validated Canadian postal-code type with canonical output and B-tree ordering. The authoritative version 1.0 SQL names the type postal_code_ca; the upstream README still shows the older name canadian_postal_code and should not be copied literally.

Core Workflow

Use the type for fields that must reject malformed postal codes and display them consistently:

CREATE EXTENSION pg_postal_code_ca;

CREATE TABLE locations (
  id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  postal_code postal_code_ca NOT NULL
);

INSERT INTO locations (postal_code) VALUES ('h2e3f1');

SELECT postal_code
FROM locations
WHERE postal_code >= 'H2E 3F0'::postal_code_ca
ORDER BY postal_code;

Input without a space is normalized to uppercase canonical form such as H2E 3F1. Invalid letter/digit placement is rejected during the cast.

Objects

  • postal_code_ca: fixed-length C type with input/output functions.
  • <, <=, =, <>, >=, and >: comparison operators.
  • postal_code_ca_cmp(postal_code_ca, postal_code_ca): three-way comparison support function.
  • postal_code_ca_ops: default B-tree operator class, enabling ordered indexes, equality lookups, range predicates, and sorting.

Caveats

This type validates the extension’s implemented Canadian postal-code grammar; it does not prove that a code is assigned, deliverable, or current. Validate against an authoritative address data source when existence matters.

The README/type-name mismatch reflects an upstream rename: use postal_code_ca from the installed SQL and verify existing schemas before migration. Comparison order is the extension’s byte-level postal-code order, not geographic proximity or routing distance.

Table reader