package stdlib

Import Path
	github.com/jackc/pgx/v5/stdlib (on go.dev)

Dependency Relation
	imports 16 packages, and imported by one package

Involved Source Files Package stdlib is the compatibility layer from pgx to database/sql. A database/sql connection can be established through sql.Open. db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test?sslmode=disable") if err != nil { return err } Or from a DSN string. db, err := sql.Open("pgx", "user=postgres password=secret host=localhost port=5432 database=pgx_test sslmode=disable") if err != nil { return err } Or a pgx.ConnConfig can be used to set configuration not accessible via connection string. In this case the pgx.ConnConfig must first be registered with the driver. This registration returns a connection string which is used with sql.Open. connConfig, _ := pgx.ParseConfig(os.Getenv("DATABASE_URL")) connConfig.Logger = myLogger connStr := stdlib.RegisterConnConfig(connConfig) db, _ := sql.Open("pgx", connStr) pgx uses standard PostgreSQL positional parameters in queries. e.g. $1, $2. It does not support named parameters. db.QueryRow("select * from users where id=$1", userID) (*sql.Conn) Raw() can be used to get a *pgx.Conn from the standard database/sql.DB connection pool. This allows operations that use pgx specific functionality. // Given db is a *sql.DB conn, err := db.Conn(context.Background()) if err != nil { // handle error from acquiring connection from DB pool } err = conn.Raw(func(driverConn any) error { conn := driverConn.(*stdlib.Conn).Conn() // conn is a *pgx.Conn // Do pgx specific stuff with conn conn.CopyFrom(...) return nil }) if err != nil { // handle error that occurred while using *pgx.Conn } # PostgreSQL Specific Data Types The pgtype package provides support for PostgreSQL specific types. *pgtype.Map.SQLScanner is an adapter that makes these types usable as a sql.Scanner. m := pgtype.NewMap() var a []int64 err := db.QueryRow("select '{1,2,3}'::bigint[]").Scan(m.SQLScanner(&a))
Package-Level Type Names (total 5)
/* sort by: | */
(*Conn) Begin() (driver.Tx, error) (*Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) (*Conn) CheckNamedValue(*driver.NamedValue) error (*Conn) Close() error Conn returns the underlying *pgx.Conn (*Conn) ExecContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Result, error) (*Conn) Ping(ctx context.Context) error (*Conn) Prepare(query string) (driver.Stmt, error) (*Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) (*Conn) QueryContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Rows, error) (*Conn) ResetSession(ctx context.Context) error *Conn : database/sql/driver.Conn *Conn : database/sql/driver.ConnBeginTx *Conn : database/sql/driver.ConnPrepareContext *Conn : database/sql/driver.ExecerContext *Conn : database/sql/driver.NamedValueChecker *Conn : database/sql/driver.Pinger *Conn : database/sql/driver.QueryerContext *Conn : database/sql/driver.SessionResetter *Conn : io.Closer
(*Driver) Open(name string) (driver.Conn, error) (*Driver) OpenConnector(name string) (driver.Connector, error) *Driver : database/sql/driver.Driver *Driver : database/sql/driver.DriverContext
OptionOpenDB options for configuring the driver when opening a new db pool. func OptionAfterConnect(ac func(context.Context, *pgx.Conn) error) OptionOpenDB func OptionBeforeConnect(bc func(context.Context, *pgx.ConnConfig) error) OptionOpenDB func OptionResetSession(rs func(context.Context, *pgx.Conn) error) OptionOpenDB func GetConnector(config pgx.ConnConfig, opts ...OptionOpenDB) driver.Connector func OpenDB(config pgx.ConnConfig, opts ...OptionOpenDB) *sql.DB
(*Rows) Close() error ColumnTypeDatabaseTypeName returns the database system type name. If the name is unknown the OID is returned. ColumnTypeLength returns the length of the column type if the column is a variable length type. If the column is not a variable length type ok should return false. ColumnTypePrecisionScale should return the precision and scale for decimal types. If not applicable, ok should be false. ColumnTypeScanType returns the value type that can be used to scan types into. (*Rows) Columns() []string (*Rows) Next(dest []driver.Value) error *Rows : database/sql/driver.Rows *Rows : database/sql/driver.RowsColumnTypeDatabaseTypeName *Rows : database/sql/driver.RowsColumnTypeLength *Rows : database/sql/driver.RowsColumnTypePrecisionScale *Rows : database/sql/driver.RowsColumnTypeScanType *Rows : io.Closer
(*Stmt) Close() error (*Stmt) Exec(argsV []driver.Value) (driver.Result, error) (*Stmt) ExecContext(ctx context.Context, argsV []driver.NamedValue) (driver.Result, error) (*Stmt) NumInput() int (*Stmt) Query(argsV []driver.Value) (driver.Rows, error) (*Stmt) QueryContext(ctx context.Context, argsV []driver.NamedValue) (driver.Rows, error) *Stmt : database/sql/driver.Stmt *Stmt : database/sql/driver.StmtExecContext *Stmt : database/sql/driver.StmtQueryContext *Stmt : io.Closer
Package-Level Functions (total 9)
GetDefaultDriver returns the driver initialized in the init function and used when the pgx driver is registered.
func OpenDB(config pgx.ConnConfig, opts ...OptionOpenDB) *sql.DB
OptionAfterConnect provides a callback for after connect.
OptionBeforeConnect provides a callback for before connect. It is passed a shallow copy of the ConnConfig that will be used to connect, so only its immediate members should be modified.
OptionResetSession provides a callback that can be used to add custom logic prior to executing a query on the connection if the connection has been used before. If ResetSessionFunc returns ErrBadConn error the connection will be discarded.
RandomizeHostOrderFunc is a BeforeConnect hook that randomizes the host order in the provided connConfig, so that a new host becomes primary each time. This is useful to distribute connections for multi-master databases like CockroachDB. If you use this you likely should set https://golang.org/pkg/database/sql/#DB.SetConnMaxLifetime as well to ensure that connections are periodically rebalanced across your nodes.
RegisterConnConfig registers a ConnConfig and returns the connection string to use with Open.
UnregisterConnConfig removes the ConnConfig registration for connStr.