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)
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
conn.CopyFrom(...)
return nil
})
if err != nil {
// handle error that occurred while using *pgx.Conn
}
Package-Level Type Names (total 11, in which 5 are exported)
// function to call on every new connection
ConnConfigpgx.ConnConfig
BuildStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
to nil to disable automatic prepared statements.
ConnConfig.Configpgconn.ConfigConnConfig.Config.BuildFrontendpgconn.BuildFrontendFuncConnConfig.Config.ConnectTimeouttime.DurationConnConfig.Config.Databasestring
// e.g. net.Dialer.DialContext
ConnConfig.Config.Fallbacks[]*pgconn.FallbackConfig
// host (e.g. localhost) or absolute path to unix domain socket directory (e.g. /private/tmp)
// e.g. net.Resolver.LookupHost
OnNotice is a callback function called when a notice response is received.
OnNotification is a callback function called when a notification from the LISTEN/NOTIFY system is received.
ConnConfig.Config.PasswordstringConnConfig.Config.Portuint16
// Run-time parameters to set on connection as session default values (e.g. search_path or application_name)
// nil disables TLS
ConnConfig.Config.Userstring
ValidateConnect is called during a connection attempt after a successful authentication with the PostgreSQL server.
It can be used to validate that the server is acceptable. If this returns an error the connection is closed and the next
fallback config is tried. This allows implementing high availability behavior such as libpq does with target_session_attrs.
ConnConfig.LogLevelpgx.LogLevelConnConfig.Loggerpgx.Logger
PreferSimpleProtocol disables implicit prepared statement usage. By default pgx automatically uses the extended
protocol. This can improve performance due to being able to use the binary format. It also does not rely on client
side parameter sanitization. However, it does incur two round-trips per query (unless using a prepared statement)
and may be incompatible proxies such as PGBouncer. Setting PreferSimpleProtocol causes the simple protocol to be
used by default. The same functionality can be controlled on a per query basis by setting
QueryExOptions.SimpleProtocol.
// Used to enforce created by ParseConfig rule.
Original connection string that was parsed into config.
// Used to enforce created by ParseConfig rule.
driver*Driver(*T) ConnString() string
Connect implement driver.Connector interface
Copy returns a deep copy of the config that is safe to use and modify.
The only exception is the tls.Config:
according to the tls.Config docs it must not be modified after creation.
Driver implement driver.Connector interface
T : database/sql/driver.Connector
Package-Level Functions (total 10, in which 7 are exported)
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.
GetDefaultDriver returns the driver initialized in the init function
and used when the pgx driver is registered.
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.