Install sql-cli on a Mac

How to install sql-cli so you can manage SQL Server right from your Mac's Terminal window.

sql-cli is a command line tool for managing SQL Server. Once you've installed SQL Server on your Mac, you can use sql-cli to connect to it and run queries.

Here are instructions for installing sql-cli on a Mac. I also demonstrate how to connect to SQL Server once you've installed it.

  1. Install sql-cli

    Open a Terminal window and run the following command to install sql-cli:

    This assumes you have NodeJs installed. If you don't, download it from Nodejs.org first. Installing NodeJs will automatically install npm which is what we use in this command to install sql-cli.

  2. Connect to SQL Server

    To connect to SQL Server, use the mssql command, followed by the username and password parameters.

    You should see something like this:

    This means you've successfully connected to SQL Server.

  3. Run a Quick Test

    Run a quick test to check that SQL Server is up and running and you can query it.

    For example, you can run the following command to see which version of SQL Server your running:

    If it's running, you should see something like this (but of course, this will depend on which version you're running):

You can now run any queries you like. For example, you can create a database:

And then see your new database included in the list of databases:

Result
name  
------
master
tempdb
model 
msdb  
Music 

5 row(s) returned

Executed in 1 ms

Another way of doing this is to execute the sp_databases stored procedure:

Result
DATABASE_NAME  DATABASE_SIZE  REMARKS
-------------  -------------  -------
master         6336           null   
model          16384          null   
msdb           15808          null   
Music          16384          null   
tempdb         16384          null   

5 row(s) returned

Executed in 1 ms

You can also use sql-cli to run scripts that contain larger SQL statements:

Result
/* Drop database if it already exists */
DROP DATABASE IF EXISTS Music;
OK

Executed in 0 ms
/* Create database */
CREATE DATABASE Music;
OK

Executed in 0 ms
/* Change to the Music database */
USE Music;
OK

Executed in 0 ms
/* Create tables */
CREATE TABLE Artists (
    ArtistId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
    ArtistName nvarchar(255) NOT NULL,
    ActiveFrom DATE NULL
);

CREATE TABLE Genres (
    GenreId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Genre nvarchar(50) NOT NULL
);

CREATE TABLE Albums
 (
  AlbumId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
  AlbumName nvarchar(255) NOT NULL,
  ReleaseDate date NOT NULL,
  ArtistId int NOT NULL,
  GenreId int NOT NULL
  
  CONSTRAINT FK_Albums_Artists FOREIGN KEY (ArtistId)     
    REFERENCES dbo.Artists (ArtistId)     
    ON DELETE NO ACTION    
    ON UPDATE NO ACTION    
);
OK

Executed in 0 ms

The SQL tutorial provides more examples of SQL statements you can use with sql-cli.

If you prefer to use a GUI tool, try installing SQL Operations Studio on your Mac. SQL Operations Studio allows you to point-and-click your way through the database objects, while still being able to run ad-hoc queries like above. It even has an integrated Terminal, which allows you to run Terminal commands right from within the interface.