Skip to content

opencc

OpenCC conversion between Simplified Chinese, Traditional Chinese, regional variants and Japanese Shinjitai

Overview

PackageVersionCategoryLicenseLanguage
pg_opencc0.1.0UTILApache-2.0 AND BSD-2-Clause AND MITC++
IDExtensionBinLibLoadCreateTrustRelocSchema
4480openccNoYesNoYesYesNoopencc

UTF8; bundled OpenCC 1.4.2.

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY0.1.01817161514pg_opencc-
RPMPIGSTY0.1.01817161514pg_opencc_$v-
DEBPIGSTY0.1.01817161514postgresql-$v-opencc-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u22.x86_64
u22.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u24.x86_64
u24.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u26.x86_64
u26.aarch64

Build

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

pig build pkg pg_opencc         # build RPM / DEB packages

Install

You can install pg_opencc 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_opencc;          # Install for current active PG version
pig
pig ext install -y pg_opencc -v 18  # PG 18
pig ext install -y pg_opencc -v 17  # PG 17
pig ext install -y pg_opencc -v 16  # PG 16
pig ext install -y pg_opencc -v 15  # PG 15
pig ext install -y pg_opencc -v 14  # PG 14
dnf
dnf install -y pg_opencc_18       # PG 18
dnf install -y pg_opencc_17       # PG 17
dnf install -y pg_opencc_16       # PG 16
dnf install -y pg_opencc_15       # PG 15
dnf install -y pg_opencc_14       # PG 14
apt
apt install -y postgresql-18-opencc   # PG 18
apt install -y postgresql-17-opencc   # PG 17
apt install -y postgresql-16-opencc   # PG 16
apt install -y postgresql-15-opencc   # PG 15
apt install -y postgresql-14-opencc   # PG 14

Create Extension:

CREATE EXTENSION opencc;

Usage

Sources:

opencc, distributed as pg_opencc, converts Simplified Chinese, Traditional Chinese, Taiwan and Hong Kong conventions, and Japanese Shinjitai. Version 0.1.0 embeds OpenCC 1.4.2 and its dictionaries in the shared library; no system OpenCC or external dictionary files are required at runtime.

Enable and convert

Supports PostgreSQL 14–18 and requires a UTF8 database. This trusted extension can be installed by a user with CREATE privilege on the database. Objects use the fixed opencc schema; the extension is not relocatable. Preloading and a server restart are not required.

CREATE EXTENSION opencc;

SELECT opencc.s2twp('鼠标里面的硅二极管坏了,导致光标分辨率降低。');
SELECT opencc.convert('繁體中文', 't2s');
SELECT * FROM opencc.configs;

opencc.convert(input, config) selects a conversion explicitly. Wrappers such as opencc.s2t() and opencc.t2s() provide one function per configuration:

ConfigurationsConversion
s2t, t2sSimplified and Traditional Chinese
s2tw, tw2s, s2hk, hk2sTaiwan or Hong Kong character conventions
s2twp, tw2spTaiwan regional phrases
s2hkp, hk2spHong Kong regional phrases; developing
t2tw, tw2t, t2hk, hk2tTraditional Chinese variant conversion
t2jp, jp2tJapanese old/new character forms; experimental

Regional conversion and inspection

opencc.localize(input, target, source) accepts zh-Hans, zh-Hant, zh-CN, zh-TW, zh-HK, and ja. Its default source => 'auto' detects the script, but cannot distinguish Taiwan from Hong Kong vocabulary. Supply the source region when regional phrases matter.

SELECT opencc.localize('滑鼠裡面的矽二極體壞了', 'zh-CN', source => 'zh-TW');
SELECT opencc.detect('中文'), opencc.script_stats('中文');
SELECT * FROM opencc.routes;
SELECT * FROM opencc.ambiguities('大战文丑的时候,他的头发很干燥', 's2t');
SELECT opencc.candidates('发', 's2t');
SELECT * FROM opencc.explain('数据库连接池', 's2twp');
ObjectPurpose
opencc.detect()Classify the script, including mixed or neutral text
opencc.script_stats()Count Han, simplified, traditional, and kana characters
opencc.configs, opencc.routesInspect configurations and regional conversion chains
opencc.ambiguities(), opencc.candidates()Inspect ambiguous spans and dictionary candidates
opencc.explain()Inspect each conversion stage
opencc.version()Report extension, engine, and dictionary digest
opencc.dependents()Find dependent database objects and suggested rebuild statements

Operation and upgrades

Conversion functions are IMMUTABLE STRICT PARALLEL SAFE within an extension version. They can be used in expression indexes and stored generated columns. PostgreSQL 18 generated columns must explicitly use STORED for user-defined conversion functions.

Dictionary changes in a future extension release can change conversion results. Inspect opencc.version() and review the statements returned by opencc.dependents() when upgrading. Dependency discovery does not find calls hidden inside other user-defined functions.

SELECT * FROM opencc.version();
SELECT * FROM opencc.dependents();

Each backend initializes converters on demand. Optional startup preloading can share their memory across backends; changing shared_preload_libraries requires a restart:

shared_preload_libraries = 'opencc'
opencc.preload = 's2t,t2s,s2twp,tw2sp'

Merge opencc into any existing preload list. opencc.preload also accepts all. Dropping the extension leaves its empty schema behind.

Was this page helpful?

Table reader