Skip to content

Getting started with opencc

Install opencc in a UTF8 PostgreSQL database and run your first script, phrase, and regional conversions in under three minutes.

1. Install opencc

Install from Pigsty packages

CI tests PostgreSQL 14–18, and Pigsty ships packages for PostgreSQL 15–19 across Linux distributions (pg_opencc_18 on EL, postgresql-18-opencc on Debian/Ubuntu). Check Downloads for exact platform coverage.

pig repo add pgsql -u
pig ext add opencc

Or build from source (PGXS)

Building from the release tag requires PostgreSQL server headers, CMake ≥ 3.12, a C++17 compiler, Python 3, and Make:

# On RHEL / Rocky / AlmaLinux (example for PG18)
sudo dnf install postgresql18-devel cmake gcc-c++ make python3
export PG_CONFIG=/usr/pgsql-18/bin/pg_config

# On Debian / Ubuntu (example for PG18)
sudo apt install postgresql-server-dev-18 cmake g++ make python3
export PG_CONFIG=/usr/lib/postgresql/18/bin/pg_config

Clone the tagged release and install (pass with_llvm=no if your pg_config enables JIT without clang installed):

git clone --branch v0.1.0 --depth 1 https://github.com/pgsty/pg_opencc.git
cd pg_opencc
make PG_CONFIG="$PG_CONFIG"
sudo make install PG_CONFIG="$PG_CONFIG"

CMake builds static OpenCC and marisa libraries and compiles the dictionaries directly into the shared library.

2. Enable in a UTF8 database

opencc is a trusted extension—any role with CREATE privilege on the database can enable it without superuser rights. The SQL schema is fixed at opencc.

SHOW server_encoding;
CREATE EXTENSION IF NOT EXISTS opencc;
SELECT * FROM opencc.version();
 server_encoding
-----------------
 UTF8

 extension | opencc |                           dictionary
-----------+--------+-----------------------------------------------------------------
 0.1.0     | 1.4.2  | sha256:...
Note

Calling text conversion in a non-UTF8 database raises opencc requires a UTF8 database.

3. Try your first conversions

  1. Context-aware phrase conversion — Notice how s2twp translates the hardware term 内存条 while leaving the classical line 海内存知己 intact:

    SELECT opencc.s2twp('海内存知己') AS poem,
           opencc.s2twp('安装内存条') AS hardware;
        poem    |    hardware
    ------------+----------------
     海內存知己 | 安裝記憶體模組
  2. Compare glyph-only vs. regional phrases — Every shorthand wrapper (such as opencc.s2t, opencc.s2twp, opencc.tw2sp) is identical to opencc.convert(input, '<config>'):

    SELECT opencc.s2t('鼠标里面的硅二极管坏了,导致光标分辨率降低。')   AS s2t,
           opencc.s2twp('鼠标里面的硅二极管坏了,导致光标分辨率降低。') AS s2twp;
                         s2t                      |                    s2twp
    ----------------------------------------------+----------------------------------------------
     鼠標裏面的硅二極管壞了,導致光標分辨率降低。 | 滑鼠裡面的矽二極體壞了,導致游標解析度降低。
  3. Target a locale with opencc.localize() — Convert one Mainland source string into multiple regional variants in one query:

    SELECT r AS target,
           opencc.localize('克里斯托弗·诺兰的电影,安装内存条', r, 'zh-CN') AS output
    FROM unnest(ARRAY['zh-Hant', 'zh-TW', 'zh-HK']) AS r;
     target  |                output
    ---------+---------------------------------------
     zh-Hant | 克里斯托弗·諾蘭的電影,安裝內存條
     zh-TW   | 克里斯多福·諾蘭的電影,安裝記憶體模組
     zh-HK   | 基斯杜化·路蘭的電影,安裝記憶體模組
  4. List all built-in configs and routes:

    SELECT name, source, target, phrases, maturity FROM opencc.configs ORDER BY name;

Non-Chinese text, emoji, empty strings, and NULL pass through unchanged.

Next steps

  • Guides and operations — Build cross-script search indexes, validate incoming text, proofread ambiguous conversions, preload dictionaries, and handle upgrades.
  • SQL reference — Look up all 16 built-in configs, function signatures, and opencc.preload.