NaviServer Database Interface – 5.1.0
nsdb - Database support and connection pool configuration
The nsdb module provides NaviServer's database integration. It connects virtual servers to globally configured database pools and exposes the database access API used by Tcl code.
Database support is optional. Installations that use it load the nsdb module for a virtual server, register one or more database drivers, define global database pools, and select which pools are available to each server.
For a detailed discussion of NaviServer database configuration, database pools, handles, and operational considerations, see Database Access and Configuration.
NaviServer database configuration consists of four parts:
The nsdb module is loaded for a virtual server.
Database drivers are registered in ns/db/drivers.
Database pools are defined globally in ns/db/pools and ns/db/pool/$dbpool.
A server selects its available pools in ns/server/$server/db.
Database pools are global definitions and may be shared by multiple virtual servers. The per-server database section determines which of these global pools are available to a server and which pool is used by default.
Load nsdb as a per-server module:
ns_section ns/server/$server/modules {
ns_param nsdb nsdb
}
Then select the database pools available to the server:
ns_section ns/server/$server/db {
ns_param pools pool1,pool2,pool3
ns_param defaultpool pool1
}
Database drivers implement the low-level connection to a specific database system. They are external NaviServer modules and must be compiled and installed separately before they can be loaded.
The ns/db/drivers section maps configuration-defined driver names to driver implementations:
ns_section ns/db/drivers {
ns_param postgres nsdbpg
ns_param nsoracle nsoracle
}
The configured driver name is later used by database pools through the driver parameter. For example, a pool using the PostgreSQL driver above would specify postgres, not nsdbpg.
ns_section ns/db/pool/pool1 {
ns_param driver postgres
}
Driver-specific settings, when needed, are configured below ns/db/driver/$dbdriver, where $dbdriver is the configured driver name from ns/db/drivers.
ns_section ns/db/driver/postgres {
ns_param pgbin /usr/lib/postgresql/16/bin/
}
Database pools define reusable sets of database connections. Each pool specifies the driver, datasource, credentials, connection count, and optional handle lifetime settings.
ns_section ns/db/pools {
ns_param pool1 "Main database pool"
ns_param pool2 "Secondary database pool"
ns_param pool3 "Optional third database pool"
}
ns_section ns/db/pool/pool1 {
ns_param driver postgres
ns_param datasource localhost::dbname
ns_param user dbuser
ns_param password secret
ns_param connections 15
ns_param logminduration 10ms
}
A pool can be used by multiple virtual servers. The server-specific ns/server/$server/db section controls which pools are available to a particular server.
The ns/server/$server/db section selects the database pools available to a virtual server.
ns_section ns/server/$server/db {
ns_param pools pool1,pool2,pool3
ns_param defaultpool pool1
}
The defaultpool is used when application code requests a database handle without naming a specific pool.
The following sections provide the generated parameter reference for database configuration.
The ns/server/$server/db section selects the database pools available to a virtual server. The pools themselves are defined globally under ns/db/pools and ns/db/pool/$dbpool and may be shared by multiple servers.
The default pool is used when application code requests a database handle without explicitly naming a pool.
ns_section ns/server/$server/db {
ns_param pools pool1,pool2,pool3
ns_param defaultpool pool1
}
Default database pool used by this server when no explicit pool name is requested
Type: string
List of database pool names available to this server; the names must refer to pools defined in ns/db/pools
Type: list
The ns/db/drivers section defines database driver instances available to NaviServer's nsdb interface. Each parameter name is a configuration-defined driver name, and each value is the database driver module implementation to load.
Database drivers are external NaviServer modules. They are maintained separately from the core server, usually in separate source repositories, and must be compiled and installed before they can be loaded. Common drivers include nsdbpg for PostgreSQL and nsoracle for Oracle; additional drivers are available for other database systems.
ns_section ns/db/drivers {
ns_param postgres nsdbpg
ns_param nsoracle nsoracle
}
Available external database drivers include nsdbpg, nsoracle, nsdbmysql, nsdbsqlite, nsdbtds, nsodbc, and nsdbbdb.
Configuration-defined database driver name whose value names the installed database driver module implementation; the driver name is used in ns/db/pool/$dbpool via the driver parameter
Value type: module
Role of parameter name: configuration-defined identifier
Cardinality: multiple entries with different parameter names are expected
The ns/db/driver/$dbdriver section contains optional settings for a database driver instance. The final path component is the configuration-defined driver name from ns/db/drivers, not necessarily the implementation name.
For example, a configuration may map the driver name postgres to the nsdbpg implementation and then configure driver-specific settings below ns/db/driver/postgres.
ns_section ns/db/drivers {
ns_param postgres nsdbpg
}
ns_section ns/db/driver/postgres {
ns_param pgbin /usr/lib/postgresql/18/bin/
}
The ns/db/pools section defines global database pool names. Each parameter name is a configuration-defined pool name, and each value is a human-readable description of the pool.
Database pools are process-wide definitions and can be made available to one or more virtual servers through the server-specific ns/server/$server/db section.
ns_section ns/db/pools {
ns_param pool1 "Main database pool"
ns_param pool2 "Secondary database pool"
ns_param pool3 "Optional third database pool"
}
Configuration-defined database pool name whose value is a human-readable pool description; the pool name is used in ns/db/pool/$dbpool sections and in per-server database configuration
Value type: string
Role of parameter name: configuration-defined identifier
Cardinality: multiple entries with different parameter names are expected
The ns/db/pool/$dbpool section configures a global database connection pool. A pool defines how NaviServer connects to a database, how many handles are maintained, and how idle or stale handles are checked and closed.
Database pools are global and may be used by multiple virtual servers. A server selects the pools it uses in ns/server/$server/db.
ns_section ns/db/pool/pool1 {
ns_param driver postgres
ns_param datasource localhost::dbname
ns_param user dbuser
ns_param password secret
ns_param connections 15
ns_param logminduration 10ms
}
Interval at which the pool checks for stale database handles
Type: time
Default: 5m
Number of database connections maintained by this pool
Type: integer
Default: 2
Driver-specific datasource string identifying the database connection target
Type: string
Database driver used by this pool
Type: string
When SQL logging is enabled, log only statements whose execution time is at least this duration
Type: time
Default: 0ms
Log SQL errors reported by this pool
Type: boolean
Default: false
Close database handles that have been idle for at least this interval
Type: time
Default: 5m
Close database handles that have been open longer than this interval
Type: time
Default: 60m
Database password used when opening connections for this pool
Type: string
Database user name used when opening connections for this pool
Type: string
The database API is documented in separate command manpages:
Command ns_db: Database access API for obtaining handles, executing SQL, iterating over result rows, transaction handling, and related database operations.
Command ns_buildsqldate: SQL date and time manipulation commands.
Command ns_dbquotevalues: Quote database names and values for use in SQL statements.
NaviServer Database