Clone wiki

devoops / How To Connect Azure SQL Database From Ubuntu

In Ubuntu 14.04 LTS

TDS Driver

Microsoft SQL Server uses Tabular Data Stream (TDS) to transfer data between a database server and a client. is an open source TDS implementation.


$ sudo apt-get install -y freetds-dev freetds-bin
$ tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v0.91
             freetds.conf directory: /etc/freetds
     MS db-lib source compatibility: no
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 4.2
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes

Configuration files (see man freetds.conf for details) are read in following order and only first found file is used:

  • $HOME/.freetds.conf
  • /etc/freetds/freetds.conf


tds version = 7.1
;dump file = freetds.log
;debug flags = 0xffff

host = <HOST>
port = 1433

Testing connections with tsql:

# everything in command line
# using configuration, note username -U requires also @<HOST> part !

ODBC Driver



$ sudo apt-get -y install tdsodbc unixodbc

Configure the driver in /etc/odbcinst.ini:

Description = FreeTDS Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/

Configure data source names (DSN) in /etc/odbc.ini (TODO: $HOME/.odbc.ini ?):

Driver = FreeTDS
Servername = <SERVERNAME> # from freetds.conf
Port = 1433
Database = <DBNAME>

Test connection with isql:


$ isql -v <DSN> <USER>@<HOST> <PASSWORD>

Command Line Tools

sqsh looks promising cmdline tool but I didn't get it working with Azure:


$ sudo apt-get install -y sqsh

sql-cli is a node.js based cross platform command line interface for SQL Server:


$ sudo apt-get install -y nodejs-legacy npm
$ sudo npm install -g sql-cli
$ mssql -s <SERVER_ADDRESS> -d <DBNAME> -u <USER>@<SERVER> -p <PASSWORD> -e

isql (part of unixodbc package). Note requires ODBC configuration before works.


$ sudo apt-get install -y unixodbc
# configure the driver and data source names
$ isql -v <DSN> <USER>@<HOST> <PASSWORD>

Other Tools

Connectivity From Ruby

Ubuntu 14.04 has Ruby 1.9.3.

The stack is:

  1. FreeTDS driver
  2. FreeTDS Ruby wrapper:

Note that no FreeTDS configuration is needed when using tiny_tds.


$ sudo gem install tiny_tds

require 'tiny_tds'

client =
  :azure => true,
  :host => '<SERVER_ADDRESS>',
  :database => '<DBNAME>',
  :username => '<USERNAME>@<HOSTNAME>',
  :password => '<PASSWORD>'

round(((100 - 1 - 1) * rand() + 1), 0) as [random_number]

result = client.execute(RANDOM_NUMBER_SQL)
result.each do |row|
  puts "random_number = %{random_number}" % { :random_number => row['random_number'] }

Connectivity From Python

Ubuntu 14.04 has Python 2.7.6.

The stack is:

  1. FreeTDS Driver
  2. ODBC Driver
  3. pyodbc

Another alternative is pymssql but I haven't tried that one.

Note that both FreeTDS and ODBC have to be configured before ODBC data source names (DSN) works.


$ sudo apt-get -y install python-pyodbc

import pyodbc

connection = pyodbc.connect('DSN=<DSN>;UID=<USERNAME>@<HOST>;PWD=<PASSWORD>')

round(((100 - 1 - 1) * rand() + 1), 0) as [random_number]

cursor = connection.cursor()

for row in cursor.execute(RANDOM_NUMBER_SQL):
    print 'random_number = {random_number:.0f}'.format(random_number=row.random_number)