Drivers and dialects

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

PostgreSQL

pgdriver

Bun comes with its own PostgreSQL driver called pgdriveropen in new window.

import "github.com/uptrace/bun/driver/pgdriver"

dsn := "postgres://postgres:@localhost:5432/test?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))

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

See PostgreSQL section for more information about pgdriver and PostgreSQL.

pgx

Alternatively, you can use pgxopen in new window with pgdialect. You can disable prepared statements in pgx, because Bun does not benefit from using them:

import (
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/pgdialect"
	"github.com/jackc/pgx/v4/stdlib"
)

config, err := pgx.ParseConfig("postgres://postgres:@localhost:5432/test?sslmode=disable")
if err != nil {
	panic(err)
}
config.PreferSimpleProtocol = true

sqldb := stdlib.OpenDB(*config)
db := bun.NewDB(sqldb, pgdialect.New())

See PostgreSQL section for more information about pgx and PostgreSQL.

MySQL5+ and MariaDB

To connect to a MySQL database, use 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())

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)