ForkMicrosoftMicrosoftpublished Mar 30, 2022seen Aug 8

microsoft/go-mssqldb

forked from denisenkom/go-mssqldb

Open original ↗

Captured source

source ↗
published Mar 30, 2022seen Aug 8captured Aug 8http 200method plain

microsoft/go-mssqldb

Description: Microsoft SQL server driver written in go language

Language: Go

License: BSD-3-Clause

Stars: 407

Forks: 99

Open issues: 52

Created: 2022-03-30T14:10:52Z

Pushed: 2026-08-08T02:37:22Z

Default branch: main

Fork: yes

Parent repository: denisenkom/go-mssqldb

Archived: no

README:

Microsoft's official Go MSSQL driver

![Go Reference](https://pkg.go.dev/github.com/microsoft/go-mssqldb) ![Build Status](https://github.com/microsoft/go-mssqldb/actions/workflows/pr-validation.yml) ![codecov](https://codecov.io/gh/microsoft/go-mssqldb)

A pure Go database/sql driver for Microsoft SQL Server and Azure SQL Database. This is the recommended Go SQL Server driver for connecting Go applications to SQL Server, Azure SQL Database, Azure SQL Managed Instance, and Azure Synapse Analytics.

Keywords: golang sql server driver, go mssql, azure sql go, go-mssqldb, sql server golang, mssql go driver

Install

Requires Go 1.25 or above.

Install with go get github.com/microsoft/go-mssqldb@latest.

Connection Parameters and DSN

The recommended connection string uses a URL format: sqlserver://username:password@host/instance?param1=value&param2=value Other supported formats are listed below.

All connection string parameters are case-insensitive. Providing the same parameter more than once with different casing (e.g., TrustServerCertificate and trustservercertificate) will result in a parse error.

Common parameters

  • user id - enter the SQL Server Authentication user id or the Windows Authentication user id in the DOMAIN\User format. On Windows, if user id is empty or missing Single-Sign-On is used. The user domain sensitive to the case which is defined in the connection string.
  • password
  • database
  • connection timeout - in seconds (default is 0 for no timeout), set to 0 for no timeout. Recommended to set to 0 and use context to manage query and connection timeouts.
  • dial timeout - in seconds (default is 15 times the number of registered protocols), set to 0 for no timeout.
  • encrypt
  • strict - Data sent between client and server is encrypted E2E using TDS8.
  • disable - Data send between client and server is not encrypted.
  • false/optional/no/0/f - Data sent between client and server is not encrypted beyond the login packet. (Default)
  • true/mandatory/yes/1/t - Data sent between client and server is encrypted.
  • app name - The application name (default is go-mssqldb)
  • authenticator - Can be used to specify use of a registered authentication provider. (e.g. ntlm, winsspi (on windows) or krb5 (on linux))
  • timezone - Sets the time zone used by the driver when parsing time types. For example: timezone=Asia/Shanghai. Supports IANA time zone names.

Connection parameters for ODBC and ADO style connection strings

  • server - host or host\instance (default localhost)
  • port - specifies the host\instance port (default 1433). If instance name is provided but no port, the driver will use SQL Server Browser to discover the port.

Less common parameters

  • keepAlive - in seconds; 0 to disable (default is 30)
  • failoverpartner - host or host\instance (default is no partner).
  • failoverport - used only when there is no instance in failoverpartner (default 1433)
  • failoverpartnerspn - The kerberos SPN (Service Principal Name) for the failover partner. Default is MSSQLSvc/host:(port|instance), matching how the driver generates ServerSPN.
  • packet size - in bytes; 512 to 32767 (default is 4096)
  • Encrypted connections have a maximum packet size of 16383 bytes
  • Further information on usage:
  • log - logging flags (default 0/no logging, 255 for full logging)
  • 1 log errors
  • 2 log messages
  • 4 log rows affected
  • 8 trace sql statements
  • 16 log statement parameters
  • 32 log transaction begin/end
  • 64 additional debug logs
  • 128 log retries
  • TrustServerCertificate
  • false - Server certificate is checked. Default is false if encrypt is specified.
  • true - Server certificate is not checked. Default is true if encrypt is not specified. If trust server certificate is true, driver accepts any certificate presented by the server and any host name in that certificate. In this mode, TLS is susceptible to man-in-the-middle attacks. This should be used only for testing.
  • certificate - The file path to a certificate authority (CA) certificate or server certificate for traditional X.509 chain validation. The specified certificate overrides the go platform specific CA certificates. The driver validates the certificate chain, expiry, and hostname. Supports PEM and DER formats.
  • serverCertificate - The file path to a server certificate for byte-for-byte comparison validation (new in v1.9.6). The driver validates that the server's certificate exactly matches this file, skipping chain validation, expiry checks, and hostname validation. This matches Microsoft.Data.SqlClient behavior. Cannot be used with certificate or hostnameincertificate. Supports PEM and DER formats.
  • hostNameInCertificate - Specifies the Common Name (CN) in the server certificate. Default value is the server host. Used with the certificate parameter, not applicable for serverCertificate.
  • tlsmin - Specifies the minimum TLS version for negotiating encryption with the server. Recognized values are 1.0, 1.1, 1.2, 1.3. If not set to a recognized value the default value for the tls package will be used. The default is currently 1.2.
  • ServerSPN - The kerberos SPN (Service Principal Name) for the server. Default is MSSQLSvc/host:port.
  • Workstation ID - The workstation name (default is the host name)
  • ApplicationIntent - Can be given the value ReadOnly to initiate a read-only connection to an Availability Group listener. The database must be specified when connecting with Application Intent set to ReadOnly.
  • protocol - forces use of a protocol. Make sure the corresponding package is imported.
  • columnencryption or column encryption setting - a boolean value indicating whether...

Excerpt shown — open the source for the full document.