Complete Guide to ODBC Configuration: odbc.ini vs odbcinst.ini on Linux – Driver Settings, DSN Inheritance and Best Practices


2 views

On Linux systems, ODBC configuration is managed through two primary files:

  • /etc/odbc.ini (or ~/.odbc.ini for user-specific settings)
  • /etc/odbcinst.ini

The odbc.ini file contains Data Source Name (DSN) configurations, while odbcinst.ini defines available ODBC drivers. They work in tandem:

[ODBC Data Sources]
# In odbc.ini
MyProductionDB = PostgreSQL
MyTestDB = MySQL

[MyProductionDB]
Driver = PostgreSQL
ServerName = db1.example.com
Port = 5432
Database = production

When you specify conflicting driver paths:

# odbcinst.ini
[PostgreSQL]
Description = PostgreSQL ODBC Driver
Driver = /usr/lib/psqlodbcw.so
Setup = /usr/lib/libodbcpsqlS.so

# odbc.ini
[MyDB]
Driver = /custom/path/psqlodbcw.so

The path in odbc.ini takes precedence for that specific DSN, while the odbcinst.ini path serves as the default.

While not exactly prototype-based inheritance, you can structure configurations to minimize duplication:

# odbcinst.ini
[MySQL]
Description = MySQL ODBC Driver
Driver = /usr/lib/libmyodbc8w.so
SERVER = default.mysql.com
PORT = 3306

# odbc.ini
[ProdMySQL]
Driver = MySQL
SERVER = prod.mysql.com  # Overrides the default

[DevMySQL]
Driver = MySQL           # Inherits default SERVER and PORT
DATABASE = devdb

The Driver specifies the main driver library, while Setup points to the configuration library:

[SQLite3]
Description = SQLite3 ODBC Driver
Driver = /usr/lib/libsqlite3odbc.so
Setup = /usr/lib/libsqlite3odbc.so
Threading = 2

In many cases they point to the same library, but some drivers separate these functions. The Setup library handles DSN configuration dialogs in GUI environments.

  • Store driver-independent settings in odbcinst.ini
  • Keep DSN-specific settings in odbc.ini
  • Use absolute paths for driver libraries
  • Document non-standard configurations with comments

Here's a complete example showing the relationship between both files:

# odbcinst.ini
[PostgreSQL]
Description = PostgreSQL ODBC Driver (Unicode)
Driver = /usr/lib/psqlodbcw.so
Setup = /usr/lib/libodbcpsqlS.so
UsageCount = 1

[MySQL]
Description = MySQL ODBC 8.0 Driver
Driver = /usr/lib/libmyodbc8w.so
Setup = /usr/lib/libmyodbc8S.so
UsageCount = 1

# odbc.ini
[ODBC Data Sources]
WebAppDB = PostgreSQL
AnalyticsDB = MySQL

[WebAppDB]
Driver = PostgreSQL
Servername = db.webapp.com
Port = 5432
Database = webapp_prod
Username = webuser
Password = ${DB_PASSWORD}  # Using environment variable

[AnalyticsDB]
Driver = MySQL
SERVER = analytics-db.internal
PORT = 3306
DATABASE = analytics
OPTION = 3

The configuration hierarchy works as follows:

[ODBC Data Sources]
MyProductionDB = PostgreSQL-Unicode
MyTestDB = PostgreSQL-ANSI

odbcinst.ini defines available drivers, while odbc.ini defines data sources that utilize those drivers. Think of it as class definitions (odbcinst.ini) versus object instances (odbc.ini).

When duplicate Driver paths exist:

# odbcinst.ini
[PostgreSQL]
Driver = /usr/lib/psqlodbcw.so

# odbc.ini
[MyDB]
Driver = /custom/path/psqlodbcw.so
Description = Test Database

The odbc.ini value always takes precedence. The driver path in odbcinst.ini serves as a fallback. This allows for environment-specific overrides.

While not a true prototype system, you can structure configurations like this:

# odbcinst.ini (shared settings)
[MySQL-Defaults]
Driver = /usr/lib/libmyodbc8w.so
Socket = /var/run/mysqld/mysqld.sock

# odbc.ini (environment-specific)
[ProductionDB]
DESCRIPTION = Main Production Database
SERVER = db1.example.com
# Inherits Driver and Socket from MySQL-Defaults

In odbcinst.ini sections:

[ODBC Drivers]
PostgreSQL=Installed

[PostgreSQL]
Driver = /usr/lib/psqlodbcw.so
Setup = /usr/lib/libodbcpsqlS.so
APILevel = 1
ConnectFunctions = YYN

The Driver specifies the runtime library, while Setup points to the configuration GUI library (used by tools like ODBCConfig). They often share the same path but serve different purposes.

Here's a complete setup for PostgreSQL:

# /etc/odbcinst.ini
[PostgreSQL Unicode]
Description = PostgreSQL ODBC Driver (Unicode)
Driver = /usr/lib/psqlodbcw.so
Setup = /usr/lib/libodbcpsqlS.so
FileUsage = 1

# ~/.odbc.ini
[WebAppDB]
Description = PostgreSQL Connection for Web App
Driver = PostgreSQL Unicode
Database = webapp_prod
Servername = db-web-01
Port = 5432
Protocol = 7.4-1
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No

When configurations don't work as expected:

  1. Verify file locations with odbcinst -j
  2. Check environment variables (ODBCINI, ODBCSYSINI)
  3. Test connectivity using isql -v DSN_NAME

The key is maintaining clean separation - driver definitions in odbcinst.ini and connection parameters in odbc.ini. This modular approach simplifies maintenance across environments.