Environment:173TECH Sayn Database Adapters
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Databases |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
SQLAlchemy 1.4-based multi-database environment supporting PostgreSQL, MySQL, Snowflake, Redshift, BigQuery, and SQLite with per-adapter feature flags and bulk loading.
Description
This environment defines the database connectivity layer used by SAYN for SQL task execution, cross-database copy, and data testing. It is built on SQLAlchemy 1.4.x (pinned < 2.0.0) and supports six database backends: PostgreSQL, MySQL, Snowflake, Redshift, BigQuery, and SQLite. Each adapter implements a `feature()` method that exposes a set of SQL capability flags (e.g. `CAN REPLACE TABLE`, `NEEDS CASCADE`, `NO SCHEMA SUPPORT`) which the core engine queries to generate dialect-appropriate SQL. Adapters also override `_load_data_batch()` with native bulk-loading methods where available (PostgreSQL COPY, Redshift S3+COPY, BigQuery load jobs, Snowflake PUT).
= Usage
Use this environment when a SAYN project defines database connections in `settings.yaml`. It is the mandatory prerequisite for SqlTask, CopyTask, TestTask, and any Python task that uses `self.default_db` or `self.connections`. SQLite requires no extra installation; all other databases require installing the corresponding optional pip extra.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux, macOS, or Windows | Same as Environment:173TECH_Sayn_Python_3_8_Plus |
| Python | >= 3.8.1 | Same as base environment |
| Network | Database access | Host/port reachability for remote databases |
| Cloud (Redshift) | AWS S3 bucket | Required for S3-based bulk loading |
| Cloud (BigQuery) | GCP project + service account | credentials_path or Application Default Credentials |
Dependencies
System Packages
- All packages from Environment:173TECH_Sayn_Python_3_8_Plus
- `libpq-dev` (apt) or `postgresql-libs` (brew) — for building psycopg2 from source
Python Packages
- `SQLAlchemy` >= 1.4.25, < 2.0.0 (core, already required)
Per database (optional extras):
| Database | Pip Extra | Key Package(s) | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| PostgreSQL | `sayn[postgresql]` | `psycopg2` >= 2.9.9 | PostgreSQL (no compile) | `sayn[postgresql-binary]` | `psycopg2-binary` >= 2.9.9 | MySQL | `sayn[mysql]` | `PyMySQL` >= 1.0.2 | Snowflake | `sayn[snowflake]` | `snowflake-sqlalchemy` >= 1.5.1 | BigQuery | `sayn[bigquery]` | `sqlalchemy-bigquery` >= 1.9.0, `google-cloud-bigquery-storage` >= 2.24.0 | Redshift | `sayn[redshift]` | `redshift-connector` >= 2.0.918, `sqlalchemy-redshift` >= 0.4.1 | SQLite | (core) | Built into Python | DAG visualisation | `sayn[graphviz]` | `graphviz` >= 0.19.1 |
Additional for Redshift S3 bulk loading:
- `boto3` (not a SAYN extra; must be installed separately)
- `orjson` >= 3.9.10 (core, already required)
Credentials
Credentials are defined in `settings.yaml` under the `credentials` key. The following parameters are required per database type:
- PostgreSQL: `host`, `user`, `password`, `port`, `dbname`
- MySQL: `host`, `user`, `password`, `port`, `database`
- Snowflake: `account`, `database`, `user`, `password` (or `private_key_file`), `warehouse`, `schema`, `role`, `region`
- BigQuery: `project`, `credentials_path` (optional), `location`, `dataset`
- Redshift: `host`, `user`, `password`, `port`, `dbname`; optionally `cluster_id` (IAM auth), `bucket` (S3 loading), `aws_access_key_id`, `aws_secret_access_key`, `aws_session_token`, `profile`, `region`
- SQLite: `database` (file path or `:memory:`)
All databases also support the optional `max_batch_rows` parameter (default: 50000) for controlling batch size in `load_data` and copy tasks.
Quick Install
# Install SAYN with all database drivers
pip install "sayn[all]"
# Or install specific drivers
pip install "sayn[postgresql]"
pip install "sayn[redshift]"
pip install "sayn[snowflake]"
pip install "sayn[bigquery]"
pip install "sayn[mysql]"
# For Redshift S3 bulk loading (boto3 is not included in the sayn extras)
pip install boto3
Code Evidence
Database factory from `sayn/database/creator.py:9-16`:
drivers = {
"postgresql": Postgresql,
"sqlite": Sqlite,
"mysql": Mysql,
"snowflake": Snowflake,
"redshift": Redshift,
"bigquery": Bigquery,
}
Feature flag method from `sayn/database/__init__.py:160-176`:
def feature(self, feature):
# Supported sql_features:
# - CREATE IF NOT EXISTS
# - CREATE TABLE NO PARENTHESES
# - INSERT TABLE NO PARENTHESES
# - DROP CASCADE
# - NO SET SCHEMA
# - NO ALTER INDEXES
# CAN REPLACE TABLE
# CAN REPLACE VIEW
# NEEDS CASCADE
# CANNOT ALTER INDEXES
# CANNOT SET SCHEMA
return feature in ()
Redshift IAM authentication from `sayn/database/redshift.py:120-126`:
if "cluster_id" in settings and "password" not in settings:
settings["connect_args"]["iam"] = True
settings["connect_args"]["db_user"] = settings.pop("user")
settings["connect_args"]["cluster_identifier"] = settings.pop("cluster_id")
SQLite legacy_alter_table pragma from `sayn/database/sqlite.py:27-31`:
@event.listens_for(engine, "connect")
def do_connect(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA legacy_alter_table = ON")
cursor.close()
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `No driver for {type} found` | Unsupported database type in settings.yaml | Use one of: postgresql, sqlite, mysql, snowflake, redshift, bigquery |
| `ImportError: No module named 'psycopg2'` | PostgreSQL driver missing | `pip install "sayn[postgresql]"` or `pip install "sayn[postgresql-binary]"` |
| `Error retrieving AWS access key credentials` | Redshift IAM auth missing AWS keys | Provide `aws_access_key_id` + `aws_secret_access_key`, or use `profile` |
| `schema not supported for database of type {db_type}` | Schema specified for SQLite | SQLite has `NO SCHEMA SUPPORT`; remove schema from config |
| `Table {schema}.{table} does not exists` | Target table missing for append load | Create the table first or use `replace=True` |
| `tmp_schema not supported` | tmp_schema set for schema-less database | Only use tmp_schema on databases that support schemas |
Compatibility Notes
- SQLite: No schema support (`NO SCHEMA SUPPORT` feature flag). Cannot alter indexes (`CANNOT ALTER INDEXES`). Cannot set schema (`CANNOT SET SCHEMA`). Uses `PRAGMA legacy_alter_table = ON` to prevent issues on re-runs.
- PostgreSQL: Supports `CAN REPLACE VIEW`, `NEEDS CASCADE`, `CANNOT SPECIFY DDL IN SELECT`. Uses native `COPY ... FROM STDIN CSV` for bulk loading.
- MySQL: Supports `CAN REPLACE VIEW`, `CANNOT SPECIFY DDL IN SELECT`, `TABLE RENAME CHANGES SCHEMA`. Uses INSERT for bulk loading (no native COPY).
- Snowflake: Supports `TABLE RENAME CHANGES SCHEMA`. Uses temp directory + `PUT` for bulk loading. Executes scripts via `execute_string`.
- Redshift: Supports `NEEDS CASCADE`, `CANNOT CHANGE SCHEMA`, `CANNOT SPECIFY DDL IN SELECT`. Cannot rename tables across schemas. Uses S3 + `COPY` for bulk loading (requires boto3). Custom merge template (`redshift_merge_tables.sql`).
- BigQuery: Supports `CAN REPLACE TABLE`, `CAN REPLACE VIEW`, `CANNOT CHANGE SCHEMA`. Uses `LOAD` jobs with NEWLINE_DELIMITED_JSON. Custom introspection via `INFORMATION_SCHEMA`. UUID type mapped to String.
|}