# Getting started with opencc

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

---

LLMS index: [llms.txt](/llms.txt)

---

## 1. Install opencc {#packages}

### 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](/download/#opencc) for exact platform coverage.

```bash
pig repo add pgsql -u
pig ext add opencc
```

### Or build from source (PGXS) {#source}

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

```bash
# 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):

```bash
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 {#enable}

`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`.

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

```text
 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 {#first-try}

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

   ```sql
   SELECT opencc.s2twp('海内存知己') AS poem,
          opencc.s2twp('安装内存条') AS hardware;
   ```

   ```text
       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>')`:

   ```sql
   SELECT opencc.s2t('鼠标里面的硅二极管坏了，导致光标分辨率降低。')   AS s2t,
          opencc.s2twp('鼠标里面的硅二极管坏了，导致光标分辨率降低。') AS s2twp;
   ```

   ```text
                        s2t                      |                    s2twp
   ----------------------------------------------+----------------------------------------------
    鼠標裏面的硅二極管壞了，導致光標分辨率降低。 | 滑鼠裡面的矽二極體壞了，導致游標解析度降低。
   ```

3. **Target a locale with `opencc.localize()`** — Convert one Mainland source string into multiple regional variants in one query:

   ```sql
   SELECT r AS target,
          opencc.localize('克里斯托弗·诺兰的电影，安装内存条', r, 'zh-CN') AS output
   FROM unnest(ARRAY['zh-Hant', 'zh-TW', 'zh-HK']) AS r;
   ```

   ```text
    target  |                output
   ---------+---------------------------------------
    zh-Hant | 克里斯托弗·諾蘭的電影，安裝內存條
    zh-TW   | 克里斯多福·諾蘭的電影，安裝記憶體模組
    zh-HK   | 基斯杜化·路蘭的電影，安裝記憶體模組
   ```

4. **List all built-in configs and routes**:

   ```sql
   SELECT name, source, target, phrases, maturity FROM opencc.configs ORDER BY name;
   ```
{.steps}

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

## Next steps {#next}

- [Guides and operations](/docs/opencc/guide/) — Build cross-script search indexes, validate incoming text, proofread ambiguous conversions, preload dictionaries, and handle upgrades.
- [SQL reference](/docs/opencc/reference/) — Look up all 16 built-in configs, function signatures, and `opencc.preload`.

---

Backlinks:

- [Package downloads](/download/)
