# Install and initialize

> Prepare a matching server and extension set, then initialize one physical database using psql.

---

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

---

## Check the installation {#components}

Use a Linux installation of the [matching Mimus kernel and extensions](/docs/mssql/#version). The PostgreSQL 19 version string alone cannot identify the patched ABI. If building from source, compile the complete kernel with ICU, OpenSSL, libxml2 and libuuid support, install its `contrib/uuid-ossp`, then build the four Babelfish modules against **that installation’s** `pg_config` and its matching `PG_SRC`. The T-SQL parser uses Java, CMake and the ANTLR 4.13.2 C++ runtime. The isolated upstream port and the Mimus build use different TDS interfaces. [Upstream build dependencies](https://github.com/babelfish-for-postgresql/babelfish_extensions/blob/a6e79da1b28c72e4801abbc9b58f946c10fcb6f9/contrib/README.md)

| Extension | SQL version | What it installs or provides |
| --- | --- | --- |
| `babelfishpg_common` | 7.0.0 | SQL Server types, casts and collations in `sys`. Its control file declares no extension dependencies, but its monetary functions load the money shared library. |
| `babelfishpg_tsql` | 7.0.0 | T-SQL parser, procedural language, functions, system catalogs and initialization. Requires `babelfishpg_common` and `uuid-ossp`. |
| `babelfishpg_tds` | 1.0.0 | TDS listener, login and packets. Preload the library at startup, then create the SQL extension in the target database. Requires `babelfishpg_tsql`. |
| `babelfishpg_money` | 1.1.0 | Standalone fixed-decimal monetary extension. The tested installation creates it explicitly, outside the TDS dependency chain. |

## Start an empty instance {#cluster}

These commands assume a fresh, isolated Linux host or container with ports 5432 and 1433 available. In the shell, run as the OS account `postgres`. Put the Mimus `bin` directory first in `PATH`. Keep this shell for the rest of the guide so `PGHOST` selects the private Unix socket. The final command should report `server started`.

```sh
export PGHOST="$(mktemp -d)" PGPORT=5432 PGUSER=postgres
export PGDATA="$PGHOST/data"
initdb -D "$PGDATA" -U postgres --encoding=UTF8 --locale=C.UTF-8 \
  --auth-local=peer --auth-host=scram-sha-256 \
  -c "unix_socket_directories=$PGHOST"
pg_ctl -D "$PGDATA" -l "$PGHOST/server.log" -w start
```

## Configure the target through psql {#configure}

Connect to the **PostgreSQL** endpoint as cluster superuser `postgres`. In the listing below, the first line opens interactive `psql`. Run the SQL and backslash commands at its prompt. Enter a new password twice at `\password` and keep it for the TDS login.

```sh
psql -X -v ON_ERROR_STOP=1 -d postgres
```

```sql
ALTER SYSTEM SET shared_preload_libraries = 'babelfishpg_tds';
ALTER SYSTEM SET listen_addresses = '127.0.0.1';
ALTER SYSTEM SET babelfishpg_tds.port = '1433';
ALTER SYSTEM SET babelfishpg_tsql.database_name = 'wire';
CREATE ROLE bbf_admin LOGIN CREATEDB CREATEROLE INHERIT;
\password bbf_admin
CREATE DATABASE wire OWNER bbf_admin;
ALTER DATABASE wire SET babelfishpg_tsql.migration_mode = 'single-db';
\q
```

Expect `ALTER SYSTEM`, `CREATE ROLE`, `CREATE DATABASE` and `ALTER DATABASE`. The `bbf_admin` login owns the physical database and becomes the initial T-SQL administrator. The example ran with `rolsuper = false`. Give applications their own logins and database users. The preload list here is for an empty instance. On a configured cluster, retain its existing required libraries when adding `babelfishpg_tds`.

As the OS account `postgres`, put this entry **before** the general host rules in `$PGDATA/pg_hba.conf`. The database field is the physical database `wire`. This password-only, loopback rule is for the local exercise. For a remote client use [TLS with certificate validation](/docs/mssql/connect/#tls).

```text
host  wire  bbf_admin  127.0.0.1/32  password
```

Restart this instance as its OS owner. Restart loads the TDS library and binds the ports. A reload alone cannot apply either change. A fresh connection to `wire` also picks up its database-level migration mode. Choose the mode before initialization. [Mapping in each mode](/docs/mssql/mapping/#modes)

```sh
pg_ctl -D "$PGDATA" -l "$PGHOST/server.log" -w restart
psql -X -v ON_ERROR_STOP=1 -d wire
```

## Initialize the physical database once {#initialize}

In the new `psql` session as PG superuser `postgres` in `wire`, run the following commands. The three settings should read `babelfishpg_tds`, `wire` and `single-db`. The installation setting applies only to this session. `CASCADE` installs the three declared dependencies. The final query should list the four versions above plus `uuid-ossp 1.1`.

```sql
SHOW shared_preload_libraries;
SHOW babelfishpg_tsql.database_name;
SHOW babelfishpg_tsql.migration_mode;
SET allow_system_table_mods = on;
CREATE EXTENSION babelfishpg_money;
CREATE EXTENSION babelfishpg_tds CASCADE;
GRANT ALL ON SCHEMA sys TO bbf_admin;
CALL sys.initialize_babelfish('bbf_admin');
RESET allow_system_table_mods;
SELECT extname, extversion FROM pg_extension
WHERE extname LIKE 'babelfishpg_%' OR extname = 'uuid-ossp' ORDER BY extname;
\q
```

`CALL` creates cluster roles and the built-in logical databases `master`, `tempdb` and `msdb`. Its argument must name the physical database owner. Once it succeeds, connect using `bbf_admin` on the TDS port. [First TDS connection](/docs/mssql/connect/) · [Upstream initialization procedure](https://github.com/babelfish-for-postgresql/babelfish_extensions/blob/a6e79da1b28c72e4801abbc9b58f946c10fcb6f9/contrib/babelfishpg_tsql/sql/ownership.sql)

## Build from paired source checkouts {#build}

The two tree checks here reproduce the earlier connection example. For the TDS encryption guides, use the paired sources with the kernel, Babelfish and pg_tde increments. [Identify the four-change candidate](/docs/patches/mssql/#tde-increments)

If the server and modules are already installed, start at [the empty instance](#cluster). Otherwise run this in a build-user shell with writable, clean checkouts of the indicated trees and a fresh install prefix. Replace all three paths. Install the development dependencies above plus GNU make, a C/C++17 toolchain, Bison, Flex and the ANTLR runtime under /usr/local first. The tree checks exit silently on a match. At the end, the prefix contains the server, PGXS and extension files. Make that prefix and the runtime libraries accessible to the server OS account.

```sh
set -eu
export PG_SRC=/path/to/mimus BBF_SRC=/path/to/babelfish_extensions
prefix=/path/to/empty/install
test "$(git -C "$PG_SRC" rev-parse 'HEAD^{tree}')" = 98c4dca860f0db13905de73d79d73cd31d88848c
test "$(git -C "$BBF_SRC" rev-parse 'HEAD^{tree}')" = 7ae1f519a130a95848fab32866ab612f7ee6b7f8

(cd "$PG_SRC" && ./configure --prefix="$prefix" --with-icu --with-ssl=openssl \
    --with-libxml --with-uuid=e2fs && make -j4 && make install)
make -C "$PG_SRC/contrib/uuid-ossp" install

export PG_CONFIG="$prefix/bin/pg_config" cmake=cmake
export PATH="$prefix/bin:$PATH"
export CPLUS_INCLUDE_PATH="/usr/local/include/antlr4-runtime${CPLUS_INCLUDE_PATH:+:$CPLUS_INCLUDE_PATH}"
tsql_src="$BBF_SRC/contrib/babelfishpg_tsql"
make -C "$tsql_src" src/pltsql_node/pltsql_nodetags.h src/pl_gram.h
make -C "$tsql_src" -j4 antlr/libantlr_tsql.a
for module in money common tds tsql; do
    make -C "$BBF_SRC/contrib/babelfishpg_$module" -j4
    make -C "$BBF_SRC/contrib/babelfishpg_$module" install
done
```

---

Backlinks:

- [SQL Server](/docs/mssql/)
- [First TDS connection](/docs/mssql/connect/)
- [Create encrypted permanent tables over TDS](/docs/mssql/encrypted-tables/)
- [Base · PostgreSQL 19.0](/docs/patches/base/)
