# Databases, schemas and roles

> Resolve logical T-SQL names to real PostgreSQL objects and manage each kind of principal from its own endpoint.

---

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

---

## Choose the mapping before initialization {#modes}

One PostgreSQL cluster hosts one initialized Babelfish physical database, selected by `babelfishpg_tsql.database_name`. All TDS connections enter that database. The login packet and T-SQL `USE` select a logical database inside it. Thus the HBA entry and `psql -d` name `wire`, while the driver names `master` or a user database such as `sales`.

| At the TDS endpoint | Native PG schema in `single-db` | Native PG schema in `multi-db` |
| --- | --- | --- |
| `master.dbo` | `master_dbo` | `master_dbo` |
| `tempdb.dbo` / `msdb.dbo` | `tempdb_dbo` / `msdb_dbo` | `tempdb_dbo` / `msdb_dbo` |
| `sales.dbo` | `dbo` | `sales_dbo` |
| `sales.billing` | `billing` | `sales_billing` |

`single-db` admits one user database in addition to the built-ins. Choose `multi-db` when several user databases need separate namespaces in one physical database. Each mode above was initialized in a fresh instance. FreeTDS created `sales.dbo.orders` and read one row in both. Changing the parameter after initialization would leave existing names and metadata in the old layout. To change layouts, provision a fresh target and migrate the application objects and data.

## Look up a name from PostgreSQL {#lookup}

At the TDS endpoint, `dbo.mimus_probe` is a two-part name relative to the current database. `master.dbo.mimus_probe` specifies all three parts. At the native endpoint, connect to `wire` and qualify the physical schema. Shared schemas have special rules (for example, T-SQL `INFORMATION_SCHEMA` maps to `information_schema_tsql`). Long physical identifiers can be shortened with a hash. Read the mapping catalog when writing native reports or grants. [Upstream schema and user mapping](https://github.com/babelfish-for-postgresql/babelfish_extensions/blob/a6e79da1b28c72e4801abbc9b58f946c10fcb6f9/contrib/babelfishpg_tsql/src/multidb.c)

In native `psql`, connected as `postgres` to the initialized `wire`, this query returns `master | dbo | master_dbo`. Change the filter to inspect a user schema.

```sql
SELECT d.name AS logical_db, n.orig_name AS logical_schema, n.nspname AS pg_schema
FROM sys.babelfish_namespace_ext AS n JOIN sys.babelfish_sysdatabases AS d USING (dbid)
WHERE n.orig_name = 'dbo' AND d.name = 'master';
```

## Separate logins from database users {#roles}

A server login authenticates the connection. A logical database user supplies the identity for grants in that database. Use the TDS endpoint for `CREATE LOGIN`, `CREATE USER … FOR LOGIN`, role membership, and `ALTER LOGIN` or `DROP LOGIN`. Grant the application’s database user access to the specific schema and objects it needs. Use `sys.server_principals` and `sys.database_principals` to inspect the two scopes. Use native PG tools for native roles.

The initializer checks the physical owner and reserves cluster-wide roles, including `sysadmin`, `bbf_role_admin` and the built-in databases’ guest roles. On an earlier integrated PG19 build with this same TDS extension, native superusers could be managed before and after initialization, in both the initialized physical database and the ordinary `postgres` database. A real TDS login remained protected against a native `DROP ROLE` in both databases. Mimus distinguishes actual guest-role membership from the implicit privileges of a PG superuser. Create a separate PG database for an unrelated application if needed. The roles still belong to the cluster. [Upstream ownership catalogs](https://github.com/babelfish-for-postgresql/babelfish_extensions/blob/a6e79da1b28c72e4801abbc9b58f946c10fcb6f9/contrib/babelfishpg_tsql/sql/ownership.sql)

---

Backlinks:

- [SQL Server](/docs/mssql/)
- [Create encrypted permanent tables over TDS](/docs/mssql/encrypted-tables/)
- [Install and initialize](/docs/mssql/install/)
- [Migration and operations](/docs/mssql/operations/)
- [Known limits and troubleshooting](/docs/mssql/status/)
