HowtoGo
Home / Standard Library / The database/sql Package
Standard Library

The database/sql Package

database/sql is a connection pool and a query interface. It speaks no SQL dialect of its own: a driver does the talking, and this package manages the connections, the transactions, and the scanning.

Examples

Open does not connect. It validates the driver name and returns a pool, so the first real failure shows up on Ping or on the first query.

import (
    "database/sql"
    _ "github.com/jackc/pgx/v5/stdlib" // registers "pgx"
)

db, err := sql.Open("pgx", dsn)
if err != nil {
    return nil, err
}

db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(5 * time.Minute)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
    return nil, err // this is where a bad DSN shows up
}
Output
connected, pool capped at 25
FunctionDescription
Open(driverName, dataSourceName string) (*DB, error)
Creates a DB handle. It does not connect, so an error here means a bad driver name, not an unreachable database.
type DB struct
A pool of connections, safe for concurrent use. Create one per database and share it.
(*DB) PingContext(ctx) error
Opens a connection to verify the database is actually reachable.
(*DB) QueryRowContext(ctx, query, args...) *Row
db.QueryRowContext(ctx, q, id).Scan(&u.ID)
One row. Errors surface from Scan, not here.
(*DB) QueryContext(ctx, query, args...) (*Rows, error)
Many rows. The returned Rows holds a connection until closed.
(*DB) ExecContext(ctx, query, args...) (Result, error)
A statement returning no rows: INSERT, UPDATE, DELETE, DDL.
(*DB) PrepareContext(ctx, query) (*Stmt, error)
A prepared statement, reusable across calls. Close it when done.
(*DB) BeginTx(ctx, opts *TxOptions) (*Tx, error)
Starts a transaction on one connection.
(*DB) SetMaxOpenConns(n int)
Caps total connections. Unlimited by default, which is rarely what you want.
(*DB) SetMaxIdleConns(n int)
How many idle connections to keep. Set it equal to MaxOpenConns to avoid churn.
(*DB) SetConnMaxLifetime(d time.Duration)
Retires a connection after this long, which lets load balancers and failovers take effect.
(*DB) SetConnMaxIdleTime(d time.Duration)
Closes a connection that has sat idle this long.
(*DB) Stats() DBStats
Live pool counters: open, in use, idle, and how often callers waited.
type Rows struct
An iterator over a result set. Next, Scan, Err, Close.
(*Rows) Next() bool
Advances to the next row. Returns false at the end and on error, which is why Err must be checked.
(*Rows) Scan(dest ...any) error
Copies the current row's columns into the destinations, in order.
(*Rows) Err() error
The error that ended the iteration, if any. Skipping this hides read failures as empty results.
type Tx struct
A transaction. Commit or Rollback exactly once.
type Result interface
LastInsertId() (int64, error) and RowsAffected() (int64, error). Postgres does not support LastInsertId.
ErrNoRows
errors.Is(err, sql.ErrNoRows)
Returned by Row.Scan when the query matched nothing. Match it with errors.Is.
NullString, NullInt64, NullBool, NullTime, NullFloat64
Wrappers for columns that can be NULL. Each carries the value plus a Valid bool.
Named(name string, value any) NamedArg
sql.Named("id", 7)
A named parameter, for drivers that support them.
type Scanner interface
Scan(src any) error. Implement it and your type can be a Scan destination.
driver.Valuer
Value() (driver.Value, error). Implement it and your type can be a query argument.
Related: context errors time