'R odbc::odbcListDrivers() does not list dirver in /opt/homebrew/etc/odbcinst.ini
I am trying to connect to a Microsoft SQL Server database from R studio.
I installed the unixODBC driver manager using homebrew:
brew install unixodbc
I then tried to install the freetds driver as recommended on https://db.rstudio.com/best-practices/drivers/
# SQL Server ODBC Drivers (Free TDS)
brew install freetds --with-unixodbc
I received the error:
Error: invalid option: --with-unixodbc
I found a source saying the option is no longer available because it is default now. Thus, I ran:
brew install freetds
In R Studio (and R) I then attempted to list the installed driver, but it was not found:
> odbc::odbcListDrivers()
[1] name attribute value
<0 rows> (or 0-length row.names)
Next I tried to install the Microsoft ODBC driver for SQL Server (macOS) according to https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15 and as recommended on https://db.rstudio.com/databases/microsoft-sql-server/
I ran:
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
To check that this driver was registered in an odbcinst.ini file I first ran:
odbcinst -j
with result:
unixODBC 2.3.9
DRIVERS............: /opt/homebrew/etc/odbcinst.ini
SYSTEM DATA SOURCES: /opt/homebrew/etc/odbc.ini
FILE DATA SOURCES..: /opt/homebrew/etc/ODBCDataSources
USER DATA SOURCES..: /Users/Gina/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
The driver was already registered in /opt/homebrew/etc/odbcinst.ini
view /opt/homebrew/etc/odbcinst.ini
file contents (note the freetds driver does not appear to be present):
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.17.dylib
UsageCount=1
In Rstudio, I tried again to list the drivers and none were found:
> odbc::odbcListDrivers()
[1] name attribute value
<0 rows> (or 0-length row.names)
I have tried many more things, including:
copying the contents of /opt/homebrew/etc/odbcinst.ini to the user level file /Users/Gina/.odbcinst.ini
editing the /opt/homebrew/etc/odbc.ini file (which was previously empty) to include connection details (though I was planning to just specify these in the DBI::dbConnect command). New redacted file contents:
[ODBC Driver 17 for SQL Server] Driver = ODBC Driver 17 for SQL Server Servername = servername.net Database = databasename UserName = rstudioapi::askForPassword("Database user") Password = rstudioapi::askForPassword("Database password") Port = 1433
copying the contents of the /opt/homebrew/etc/odbc.ini to the user level file /Gina/.odbc.ini
setting an environmental variable:
export ODBCSYSINI=/opt/homebrew/etc
Note that when I run:
odbcinst -q -s
The driver is found:
[ODBC Driver 17 for SQL Server]
Thus, it appears that odbc::odbcListDrivers() should be able to find the driver. Any thoughts or suggestions would be very much appreciated! Thank you!
EDIT: I also tried using the New Connection button and interestingly, my driver is listed there! New Connection Window
However, when I click on it and click Test, I get an error:
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Data source name not found and no default driver specified
Driver Connection Window with Error
I get the same error when I try:
isql -v ODBC Driver 17 for SQL Server myusername mypassword
[IM002][unixODBC][Driver Manager]Data source name not found and no
default driver specified
EDIT 2: I changed the Data Source Name (top line in odbc.ini file) to "SQLSever" and changed the Servername argument to Server (I did this in both the system level file and the user level file). Now when I run the below command it tries to connect (though hits a new error).
isql -v SQLServer myusername mypassword
[08001][unixODBC][Microsoft][ODBC Driver 17 for SQL Server]SSL
Provider: [OpenSSL library could not be loaded, make sure OpenSSL
1.0 or 1.1 is installed]
[08001][unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Client
unable to establish connection
[ISQL]ERROR: Could not SQLConnect
However, unfortunately, I still can not connect in R. I get the same errors as before.
Note that it appears that the isql command uses the system level files and RStudio uses the user level files. The files are identical in both places.
Solution 1:[1]
I read in a post on a similar issue that the M1 Macs have this problem but non-M1 Macs do not. Thus, I tried repeating the whole process on an older Macbook Pro and did not encounter the problem! odbc::odbcListDrivers() immediately listed the driver after installing the Microsoft ODBC driver for SQL Server (macOS). I did not need to edit or create any files.
Solution 2:[2]
This solution worked for me:
Create two symlinks:
sudo mkdir -p /usr/local/etc
sudo ln -s /opt/homebrew/etc/odbc.ini /usr/local/etc/odbc.ini
sudo ln -s /opt/homebrew/etc/odbcinst.ini /usr/local/etc/odbcinst.ini
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gconte |
Solution 2 | Dima Lituiev |