Basic Configuration
The database connection is configured using thedatabase_url parameter, which follows this general format:
Setting the Database URL
Via environment variable:sqlpage/sqlpage.json):
Database connection URL. Can be a standard connection string (
dbengine://user:password@host:port/dbname) or an ODBC connection string.Special characters should be percent-encoded:@→%40:→%3A/→%2F?→%3F#→%23
Database password. If set, this will override any password specified in the
database_url. This allows you to keep the password separate from the connection string for better security.This password does not need to be percent-encoded.
Database-Specific Connection Strings
SQLite
SQLite is a serverless database that stores data in a single file. It’s perfect for small to medium applications. Basic connection:mode=rwc- Open in read-write mode, create if doesn’t existmode=ro- Read-only modecache=shared- Share cache between connections (required for in-memory databases)
An array of SQLite extensions to load, such as
mod_spatialite for GIS functionality.PostgreSQL
PostgreSQL is a powerful open-source relational database. Basic connection:sslmode- SSL mode:disable,allow,prefer,require,verify-ca,verify-fullsslrootcert- Path to CA certificate filesslcert- Path to client certificate filesslkey- Path to client key fileapplication_name- Application name (useful for monitoring and logging)
MySQL / MariaDB
MySQL and MariaDB are popular open-source relational databases. Basic connection:Microsoft SQL Server (MSSQL)
Microsoft SQL Server is an enterprise relational database. Basic connection:ODBC (Oracle, Snowflake, BigQuery, DB2, DuckDB, etc.)
For ODBC-compatible databases, you can use ODBC connection strings directly.ODBC drivers must be installed and configured on your system. On Linux, the unixODBC driver manager is statically linked into the SQLPage binary.
Connection Pooling
SQLPage uses connection pooling to efficiently manage database connections. Multiple requests can share a pool of connections, improving performance and resource usage.Pool Size
How many simultaneous database connections to open at most.Default values:
- PostgreSQL: 50
- MySQL: 75
- SQLite: 16
- MSSQL: 100
Connection Timeouts
Automatically close database connections after this period of inactivity. Set to 0 to disable.Default values:
- SQLite: No timeout
- All other databases: 1800 seconds (30 minutes)
Always close database connections after this amount of time. Set to 0 to disable.Default values:
- SQLite: No limit
- All other databases: 3600 seconds (60 minutes)
How long to wait when acquiring a database connection from the pool before giving up and returning an error.
Connection Retries
Database connection attempts before giving up when the server starts. Retries will happen every 5 seconds. The default of 6 retries means SQLPage will wait up to 30 seconds for the database to become available.This is useful when starting SQLPage and the database server at the same time (e.g., with Docker Compose).
Connection Management Scripts
SQLPage allows you to run SQL scripts when connections are opened or returned to the pool.on_connect.sql
Run SQL when a new database connection is opened. Place this file atsqlpage/on_connect.sql.
PostgreSQL example - Set connection parameters:
on_reset.sql
Run SQL after a request has been processed, before returning the connection to the pool. Place this file atsqlpage/on_reset.sql.
Rollback any open transactions:
is_healthy set to false:
Security Best Practices
Use separate password configuration
Use separate password configuration
Instead of embedding passwords in the connection string:This makes it easier to manage secrets and avoid accidentally committing passwords to version control.
Use SSL/TLS for remote connections
Use SSL/TLS for remote connections
Always use SSL/TLS when connecting to a remote database:
Limit connection pool size
Limit connection pool size
Set
max_database_pool_connections based on your database server’s capacity to prevent overwhelming it.Use read-only connections when possible
Use read-only connections when possible
For pages that only read data, consider creating a separate database user with read-only permissions.
Store database file outside web root
Store database file outside web root
For SQLite databases, store the
.db file in the configuration directory (default: ./sqlpage/), not in the web root where it could be downloaded.Use environment variables for sensitive data
Use environment variables for sensitive data
Don’t commit database credentials to version control. Use environment variables or a
.env file that’s excluded from git:Troubleshooting
Connection Failed
If SQLPage can’t connect to your database:- Check the connection string format - Make sure special characters are percent-encoded
- Verify database server is running - Try connecting with another tool (psql, mysql, etc.)
- Check firewall settings - Ensure the database port is accessible
- Review SSL/TLS settings - Try with
sslmode=disablefirst, then add SSL - Enable debug logging - Set
RUST_LOG=sqlpage=debugto see detailed connection attempts
Pool Exhausted
If you see “timeout waiting for connection” errors:- Increase pool size - Raise
max_database_pool_connections - Decrease timeout values - Lower
database_connection_idle_timeout_seconds - Check for connection leaks - Review your SQL code for unclosed transactions
- Add connection reset logic - Use
on_reset.sqlto rollback transactions
Slow Connections
If connections take a long time to establish:- Check network latency - Use a database closer to your SQLPage server
- Review on_connect.sql - Simplify or remove expensive initialization queries
- Disable SSL temporarily - Test if SSL negotiation is the bottleneck
- Check database server load - Monitor database server performance
Related Topics
Configuration Overview
Learn about all configuration options
Environment Variables
Complete reference of all environment variables
Migrations
Set up database migrations
Functions Reference
Database functions available in SQLPage