Skip to main content

Schemas

A SqbConnection can read and change the active schema of its underlying database session, on adapters that support it.

await client.acquire(async connection => {
const current = await connection.getSchema();
await connection.setSchema('reporting');
// ... run queries against the "reporting" schema ...
await connection.setSchema(current);
});

getSchema() / setSchema()

MethodDescription
getSchema(): Promise<string>Returns the connection's current active schema.
setSchema(schema: string): Promise<void>Switches the connection's active schema.

Both throw if called on a released connection. Both also throw <dialect> adapter does have Schema support if the underlying adapter doesn't implement schema support (getSchema/setSchema are optional methods on Adapter.Connection) — check your specific database adapter's documentation.

Setting a default schema at the client level

You can also set a default schema for every connection the pool creates by passing schema in ClientConfiguration (see Creating a Client):

const client = new SqbClient({
dialect: 'postgres',
host: 'localhost',
database: 'mydb',
schema: 'reporting',
});

This is forwarded to the adapter when it opens a new physical connection; use connection.setSchema() when you need to switch schemas on an already-open connection at runtime.

Next steps