Drivers and dialects

To connect to a database, you need a database/sql driver and a corrensponding SQL dialect that comes with bun.

PostgreSQL

See PostgreSQL section for information about using Bun with PostgreSQL.

MySQL

Bun supports MySQL 5+ and MariaDB using MySQL driveropen in new window and mysqldialect:

import (
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/mysqldialect"
    _ "github.com/go-sql-driver/mysql"
)

sqldb, err := sql.Open("mysql", "root:pass@/test")
if err != nil {
	panic(err)
}

db := bun.NewDB(sqldb, mysqldialect.New())

MSSQL

Bun supports SQL Server v2019.CU4 starting from v1.1.x. To connect to a SQL Server, use go-mssqldbopen in new window driver and mssqldialect:

import (
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/mssqldialect"
	_ "github.com/denisenkom/go-mssqldb"
)

sqldb, err := sql.Open("sqlserver", "sqlserver://sa:passWORD1@localhost:1433?database=test")
if err != nil {
	panic(err)
}

db := bun.NewDB(sqldb, mssqldialect.New())

SQLite

To connect to a SQLite database, use sqliteshimopen in new window driver which automatically imports modernc.org/sqliteopen in new window or mattn/go-sqlite3open in new window depending on your platform.

import (
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/sqlitedialect"
    "github.com/uptrace/bun/driver/sqliteshim"
)

sqldb, err := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared")
if err != nil {
	panic(err)
}

db := bun.NewDB(sqldb, sqlitedialect.New())

If you are using an in-memory database, you need to configure *sql.DB to NOT close active connections. Otherwise, the database is deleted when the connection is closed.

sqldb.SetMaxIdleConns(1000)
sqldb.SetConnMaxLifetime(0)

Oracle

To connect to an Oracle database, use go-oci8open in new window driver and oracledialect:

import (
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/oracledialect"
	_ "github.com/mattn/go-oci8"
)

sqldb, err := sql.Open("oci8", "127.0.0.1")
if err != nil {
	panic(err)
}

db := bun.NewDB(sqldb, oracledialect.New())

Writing DMBS specific code

Bun comes with featureopen in new window package that allows you to discover features supported by your DBMS:

import "github.com/uptrace/bun/dialect/feature"

if db.HasFeature(feature.InsertOnConflict) {
    // DBMS supports `ON CONFLICT DO UPDATE` (PostgreSQL, SQLite)
}

if db.HasFeature(feature.InsertOnDuplicateKey) {
    // DBMS supports `ON DUPLICATE KEY UPDATE` (MySQL, MariaDB)
}

You can also directly check the database dialect name:

import "github.com/uptrace/bun/dialect"

switch db.Dialect().Name() {
    case dialect.SQLite:
    case dialect.PG:
    case dialect.MySQL:
    case dialect.MSSQL:
    default:
        panic("not reached")
}