ODBC: Universal Database Connector
Connect flAPI to databases that have an ODBC driver but no DuckDB extension of their own: Oracle, IBM DB2 (including AS/400), Informix, Teradata, SAP HANA, Vertica and many more. The bridge is DuckDB's odbc core extension, which runs a query through an ODBC driver and returns the result as a DuckDB table.
nanodbc no longer worksEarlier versions of this guide used the nanodbc community extension with ATTACH ... (TYPE nanodbc). nanodbc has not been published since DuckDB 1.5, and flAPI moved to DuckDB 1.5 in v26.08.31 (and to DuckDB 2.0.1 in v26.11.23), so that setup does not start on any flAPI since v26.08.31. Use the core odbc extension shown here.
How it works
The extension's main function is odbc_query(connection, sql). connection is an ODBC connection string ('DSN=OracleProd') or a handle from odbc_connect(...); sql runs on the remote database, as written, in its own SQL dialect. odbc_list_drivers() and odbc_list_data_sources() show what the ODBC driver manager can see.
Step 1: Install the ODBC driver manager and a driver
Linux (Debian/Ubuntu):
sudo apt-get install unixodbc
# plus the vendor's driver, e.g. Oracle Instant Client ODBC, IBM Db2 ODBC, Teradata ODBC
macOS:
brew install unixodbc
Windows: the driver manager is part of Windows; install the vendor's driver.
Step 2: Define a DSN
[Oracle ODBC]
Description = Oracle ODBC Driver
Driver = /usr/lib/oracle/19.3/client64/lib/libsqora.so.19.1
[OracleProd]
Driver = Oracle ODBC
ServerName = oracle-prod.company.com:1521/PRODDB
UID = flapi_reader
PWD = <the password>
Neither unixODBC nor flAPI expands environment variables in odbc.ini; a ${ORACLE_PASSWORD} there is literal text. Keep credentials in a DSN file only the flAPI service user can read (chmod 600, or a per-user ~/.odbc.ini), or use your driver's own credential mechanism (an Oracle wallet, Kerberos).
Test the DSN before flAPI sees it:
isql -v OracleProd
SQL> SELECT COUNT(*) FROM HR.EMPLOYEES;
Step 3: Configure flAPI
project-name: hr-api
template:
path: ./sqls
connections:
oracle-hr:
init: |
INSTALL odbc;
LOAD odbc;
properties:
dsn: 'DSN=OracleProd'
ducklake:
enabled: true
alias: cache
metadata-path: ./cache.ducklake
data-path: ./cache_data
initruns once, at startup.odbcis a DuckDB core extension, so plainINSTALL odbc;fetches it (pre-install it withflapi extensions installfor offline hosts).properties.dsnis what templates read as{{{ conn.dsn }}}.- Keep passwords out of the connection string. When a connection fails, DuckDB's error quotes the whole connection string, and that error lands in the flAPI log.
Step 4: Cache the data, serve from the cache
odbc_query sends its second argument to the remote database as text, so a request value pasted into it would be pasted into remote SQL. The safe and fast pattern is to run odbc_query only in a cache populate template, with no request values, and let requests filter the local cache with bound parameters:
url-path: /hr/employees
request:
- field-name: department
field-in: query
required: false
validators:
- type: string
regex: '^[A-Za-z &-]{1,50}$'
- field-name: min_salary
field-in: query
required: false
validators:
- type: number
min: 0
template-source: employees.sql
connection: [oracle-hr]
cache:
enabled: true
table: oracle_employees
schedule: 6h
template-file: employees_cache.sql
CREATE OR REPLACE TABLE {{cache.catalog}}.{{cache.schema}}.{{cache.table}} AS
SELECT *
FROM odbc_query('{{{ conn.dsn }}}', '
SELECT e.employee_id, e.first_name, e.last_name, e.email, e.hire_date,
e.salary, d.department_name
FROM HR.EMPLOYEES e
JOIN HR.DEPARTMENTS d ON d.department_id = e.department_id
WHERE e.hire_date >= ADD_MONTHS(SYSDATE, -60)
')
SELECT *
FROM {{cache.catalog}}.{{cache.schema}}.{{cache.table}}
WHERE ({{ params.min_salary }} IS NULL OR salary >= {{ params.min_salary }})
{{#params.department}}
AND department_name = '{{{ params.department }}}'
{{/params.department}}
ORDER BY hire_date DESC
- The inner SQL is Oracle SQL (
ADD_MONTHS,SYSDATE): it runs on Oracle, not in DuckDB. Write''for a quote inside it. min_salaryis outside any section, so it is bound;departmentis inside one, so theregexvalidator keeps it safe.- No
LIMITin the serving template: GET endpoints are paginated, and?limit=/?offset=belong to the pagination. - Column names come back as the driver reports them; Oracle reports unquoted names in upper case. Check with
flapii templates test <path>(it needs the config service: start flAPI with--config-serviceand the sameFLAPI_CONFIG_SERVICE_TOKENexported for both) or by querying the cache table.
Incremental refresh
For large tables, declare a cursor and fetch only rows past the watermark. The first run has none, so the template bootstraps the table:
cache:
enabled: true
table: oracle_orders
schedule: 30m
cursor:
column: ORDER_ID
type: integer
template-file: orders_cache.sql
{{^cache.previousSnapshotTimestamp}}
CREATE OR REPLACE TABLE {{cache.catalog}}.{{cache.schema}}.{{cache.table}} AS
SELECT * FROM odbc_query('{{{ conn.dsn }}}', 'SELECT ORDER_ID, CUSTOMER_ID, TOTAL FROM SALES.ORDERS');
{{/cache.previousSnapshotTimestamp}}
{{#cache.previousSnapshotTimestamp}}
INSERT INTO {{cache.catalog}}.{{cache.schema}}.{{cache.table}}
SELECT * FROM odbc_query('{{{ conn.dsn }}}',
'SELECT ORDER_ID, CUSTOMER_ID, TOTAL FROM SALES.ORDERS WHERE ORDER_ID > {{{cache.previousSnapshotTimestamp}}}');
{{/cache.previousSnapshotTimestamp}}
The watermark is max(ORDER_ID) over the rows already cached, a number flAPI computes, not a request value.
Vendor SQL inside odbc_query
Because the inner SQL runs remotely as written, use the source's dialect:
-- Oracle
SELECT * FROM odbc_query('{{{ conn.dsn }}}',
'SELECT employee_id, NVL(manager_id, 0) AS manager_id FROM HR.EMPLOYEES');
-- DB2 (including AS/400)
SELECT * FROM odbc_query('{{{ conn.dsn }}}',
'SELECT * FROM PRODLIB.ITEMS WHERE CREATED >= CURRENT DATE - 30 DAYS');
-- Teradata
SELECT * FROM odbc_query('{{{ conn.dsn }}}',
'SELECT * FROM ANALYTICS.SALES WHERE SALE_DATE >= CURRENT_DATE - 90');
Filter and aggregate inside the remote SQL where you can: every row the driver returns travels over the network.
Best practices
- Use a read-only database user, granted only the tables you serve.
- Serve from a cache. Legacy databases are often slow, and the cache shields them from API traffic.
- Keep request values out of the remote SQL. Filter the cache with bound parameters instead.
- Keep credentials out of connection strings and out of the YAML.
- Filter at the source: only the columns and the date range you need.
- One process per trust boundary. Every template of a flAPI process can reach every DSN the service user can read.
Troubleshooting
| Error (in the server log) | Cause | Fix |
|---|---|---|
Data source name not found and no default driver specified | The DSN is not in odbc.ini, or flAPI runs as a user that cannot see it | Check the name (case-sensitive) and which odbc.ini the service user reads; odbcinst -j prints the paths |
Can't open lib '...' : file not found | The driver path in odbcinst.ini is wrong, or a library it needs is missing | Check the path; run ldd on the driver |
500 with only a request_id | The query failed | Look the id up in the server log (Observability) |
| Garbled characters | Driver character set | Set the driver's charset option in the DSN (for Oracle, NLS_LANG in the environment) |
| Slow refreshes | Too much data per refresh | Filter at the source, select fewer columns, use an incremental cursor |
Migration path
ODBC is often a bridge while a system is replaced. Because requests read only the cache table, moving the source changes only the populate template:
-- Phase 1: Oracle through ODBC
CREATE OR REPLACE TABLE {{cache.catalog}}.{{cache.schema}}.{{cache.table}} AS
SELECT * FROM odbc_query('{{{ conn.dsn }}}', 'SELECT ... FROM HR.EMPLOYEES');
-- Phase 2: the same data, now in PostgreSQL (attached in init as pg)
CREATE OR REPLACE TABLE {{cache.catalog}}.{{cache.schema}}.{{cache.table}} AS
SELECT ... FROM pg.hr.employees;
The endpoint, its validators and its serving template stay the same.
Next steps
- PostgreSQL: a native extension, no ODBC needed
- Caching Setup: refresh modes and schedules
- Connections Overview: live vs cache, failure modes
- unixODBC and connectionstrings.com: driver manager and connection-string reference