Skip to content

pg_try_cast

Text conversion with NULL or a typed fallback on conversion errors.

Overview

PackageVersionCategoryLicenseLanguage
pg_try_cast1.0FUNCPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
4855pg_try_castNoYesNoYesYesYes-

Release 1.0.0 installs control/SQL 1.0; META provides.version says 1.0.0. PG16+ required, CI covers 16-18; input types without soft-error support may still raise errors.

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.01817161514pg_try_cast-
RPMPIGSTY1.0.01817161514pg_try_cast_$v-
DEBPIGSTY1.0.01817161514postgresql-$v-pg-try-cast-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64N/AN/A
el8.aarch64N/AN/A
el9.x86_64N/AN/A
el9.aarch64N/AN/A
el10.x86_64N/AN/A
el10.aarch64N/AN/A
d12.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
d12.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
d13.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
d13.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
u22.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
u22.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
u24.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
u24.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
u26.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A
u26.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
N/AN/A

Build

You can build the RPM / DEB packages for pg_try_cast using pig build:

pig build pkg pg_try_cast         # build RPM / DEB packages

Install

You can install pg_try_cast directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

Install
pig install pg_try_cast;          # Install for current active PG version
pig
pig ext install -y pg_try_cast -v 18  # PG 18
pig ext install -y pg_try_cast -v 17  # PG 17
pig ext install -y pg_try_cast -v 16  # PG 16
dnf
dnf install -y pg_try_cast_18       # PG 18
dnf install -y pg_try_cast_17       # PG 17
dnf install -y pg_try_cast_16       # PG 16
apt
apt install -y postgresql-18-pg-try-cast   # PG 18
apt install -y postgresql-17-pg-try-cast   # PG 17
apt install -y postgresql-16-pg-try-cast   # PG 16

Create Extension:

CREATE EXTENSION pg_try_cast;

Usage

Sources:

pg_try_cast provides try_cast and try_convert for converting text without aborting a query on supported input errors. Release 1.0.0 installs SQL version 1.0. PostgreSQL 16 or later is required; upstream CI covers 16–18. It is trusted and relocatable, with no preload requirement.

Core Workflow

CREATE EXTENSION pg_try_cast;
SELECT try_cast('42', NULL::integer);
SELECT try_cast('invalid', 0);
SELECT try_convert(NULL::date, '2026-02-30');

Operational Boundaries

try_cast(value text, fallback anyelement) takes its result type and error fallback from the second argument; try_convert(fallback anyelement, value text) reverses the order. NULL input returns NULL. Give the fallback an explicit type. Type modifiers and domain constraints are checked. This covers text input, not arbitrary native casts. Input functions without soft-error support can still raise an error, including malformed type names passed to regtype. A role with CREATE privilege on the database can install this trusted extension after its files are installed.

Was this page helpful?