pgx: Index | Files

package stdlib

import ""

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)

In Go 1.13 and above (*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 interface{}) error {
	conn := driverConn.(*stdlib.Conn).Conn() // conn is a *pgx.Conn
	// Do pgx specific stuff with conn
	return nil
if err != nil {
	// handle error that occurred while using *pgx.Conn


Package Files



var ErrNotPgx = errors.New("not pgx *sql.DB")

func AcquireConn Uses

func AcquireConn(db *sql.DB) (*pgx.Conn, error)

AcquireConn acquires a *pgx.Conn from database/sql connection pool. It must be released with ReleaseConn.

In Go 1.13 this functionality has been incorporated into the standard library in the db.Conn.Raw() method.

func GetDefaultDriver Uses

func GetDefaultDriver() driver.Driver

GetDefaultDriver returns the driver initialized in the init function and used when the pgx driver is registered.

func OpenDB Uses

func OpenDB(config pgx.ConnConfig, opts ...OptionOpenDB) *sql.DB

func RegisterConnConfig Uses

func RegisterConnConfig(c *pgx.ConnConfig) string

RegisterConnConfig registers a ConnConfig and returns the connection string to use with Open.

func ReleaseConn Uses

func ReleaseConn(db *sql.DB, conn *pgx.Conn) error

ReleaseConn releases a *pgx.Conn acquired with AcquireConn.

func UnregisterConnConfig Uses

func UnregisterConnConfig(connStr string)

UnregisterConnConfig removes the ConnConfig registration for connStr.

type Conn Uses

type Conn struct {
    // contains filtered or unexported fields

func (*Conn) Begin Uses

func (c *Conn) Begin() (driver.Tx, error)

func (*Conn) BeginTx Uses

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

func (*Conn) CheckNamedValue Uses

func (c *Conn) CheckNamedValue(*driver.NamedValue) error

func (*Conn) Close Uses

func (c *Conn) Close() error

func (*Conn) Conn Uses

func (c *Conn) Conn() *pgx.Conn

Conn returns the underlying *pgx.Conn

func (*Conn) ExecContext Uses

func (c *Conn) ExecContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Result, error)

func (*Conn) Ping Uses

func (c *Conn) Ping(ctx context.Context) error

func (*Conn) Prepare Uses

func (c *Conn) Prepare(query string) (driver.Stmt, error)

func (*Conn) PrepareContext Uses

func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

func (*Conn) QueryContext Uses

func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Rows, error)

type Driver Uses

type Driver struct {
    // contains filtered or unexported fields

func (*Driver) Open Uses

func (d *Driver) Open(name string) (driver.Conn, error)

func (*Driver) OpenConnector Uses

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

type OptionOpenDB Uses

type OptionOpenDB func(*connector)

OptionOpenDB options for configuring the driver when opening a new db pool.

func OptionAfterConnect Uses

func OptionAfterConnect(ac func(context.Context, *pgx.Conn) error) OptionOpenDB

OptionAfterConnect provide a callback for after connect.

type Rows Uses

type Rows struct {
    // contains filtered or unexported fields

func (*Rows) Close Uses

func (r *Rows) Close() error

func (*Rows) ColumnTypeDatabaseTypeName Uses

func (r *Rows) ColumnTypeDatabaseTypeName(index int) string

ColumnTypeDatabaseTypeName returns the database system type name. If the name is unknown the OID is returned.

func (*Rows) ColumnTypeLength Uses

func (r *Rows) ColumnTypeLength(index int) (int64, bool)

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.

func (*Rows) ColumnTypePrecisionScale Uses

func (r *Rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool)

ColumnTypePrecisionScale should return the precision and scale for decimal types. If not applicable, ok should be false.

func (*Rows) ColumnTypeScanType Uses

func (r *Rows) ColumnTypeScanType(index int) reflect.Type

ColumnTypeScanType returns the value type that can be used to scan types into.

func (*Rows) Columns Uses

func (r *Rows) Columns() []string

func (*Rows) Next Uses

func (r *Rows) Next(dest []driver.Value) error

type Stmt Uses

type Stmt struct {
    // contains filtered or unexported fields

func (*Stmt) Close Uses

func (s *Stmt) Close() error

func (*Stmt) Exec Uses

func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error)

func (*Stmt) ExecContext Uses

func (s *Stmt) ExecContext(ctx context.Context, argsV []driver.NamedValue) (driver.Result, error)

func (*Stmt) NumInput Uses

func (s *Stmt) NumInput() int

func (*Stmt) Query Uses

func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error)

func (*Stmt) QueryContext Uses

func (s *Stmt) QueryContext(ctx context.Context, argsV []driver.NamedValue) (driver.Rows, error)

Package stdlib imports 15 packages (graph) and is imported by 102 packages. Updated 2020-07-18. Refresh now. Tools for package owners.